Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

Binary Search

_____________________________________
import java.util.Arrays;

public class Program01 {


public static void main(String[] args) {
int [] arr= {1,2,3,4,5};
int choiceEle=1;
int index=Arrays.binarySearch(arr, choiceEle);
if(index>=0) {
System.out.println("Element is at index :"+index);
}
else {
System.out.println("Element not found");
}
}
}

--------------------------------------------------------------------
Anonymous Array
___________________________
An array in Java without any name is known as an anonymous array. It is an array
just for creating and using instantly. Using an anonymous array, we can pass an
array with user values without the referenced variable.

public class Program02 {


public static void print(Object [] arr) {
for(Object ob: arr) {
System.out.println(ob);
}
}
public static void main(String[] args) {
// Integer [] arr= {1,2,3,4};
Program02.print(new Integer[] {1,2,3,4});
// String s[]= {"abc", "def" , "ghi"};
Program02.print(new String[] {"abc", "def" , "ghi"});
Program02.print(new Float[] {3.1f,5.2f});
}
}
---------------------------------------------------------------------
Copy the array into another array
______________________________________
package com.nit.day12;

import java.util.Arrays;

/*
* Req
* _____
* copy the old array element into the new array
* with the same old array element size or type
*/
public class Program03 {
public static void main(String[] args) {
int [] arr= {1,2,3,4,5};
int [] newArr=new int[arr.length];

for(int i=0;i<arr.length;i++) {
newArr[i]=arr[i];
}
System.out.println("Old array :");
System.out.println(Arrays.toString(arr));
System.out.println("new array :");
System.out.println(Arrays.toString(newArr));
}
}
-------------------------------------------------------------------
Merge Two array
_______________________
package com.nit.day12;

import java.util.Arrays;

/*
* Merge Two array in the single array with the same type and
* with adjustable size
*/
public class Program04 {
public static void main(String[] args) {
int arr1[]= {1,2,3};
int arr2[]= {4,5,6,7,8};

int newArr[]=new int[arr1.length+arr2.length];


int index=0;// 5
for(int i=0;i<arr1.length;i++) {
newArr[index]=arr1[i];// [ 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8]
index++;
}

for(int i=0;i<arr2.length;i++) {
newArr[index]=arr2[i];
index++;
}

System.out.println("old array 1 :"+ Arrays.toString(arr1));


System.out.println("old array 2 :"+Arrays.toString(arr2));
System.out.println("new array :"+Arrays.toString(newArr));

}
}
------------------------------------------------------------------------
package com.nit.day12;
/*
* copy one array into new array by adding
the extra content in
* the new array by using one old array and one new array new only
*
* Arrays.toString(primitive/ref);
* Arrays.sort(primitive/ref);
* Arrays.binarySearch(arr, choiceEle);
* Arrays.equals(1st array ,2nd array );
* Arrays.hashCode(primitive/ref);
* Arrays.copyOf(primitive/ref arr, length);
*/
import java.util.Arrays;
import java.util.Scanner;
public class Program05 {
public static void main(String[] args) {
int [] arr= {1,2,3,4,5,6};
Scanner sc=new Scanner(System.in);
System.out.println("Enter how many extra content you want to add");
int choice=sc.nextInt();
int [] newArr=Arrays.copyOf(arr, arr.length+choice);

for(int i=arr.length;i<newArr.length;i++) {
System.out.println("Enter the element which you want to add");
newArr[i]=sc.nextInt();
}

System.out.println("Old array :");


System.out.println(Arrays.toString(arr));
System.out.println("new array :");
System.out.println(Arrays.toString(newArr));

}
}

You might also like