Professional Documents
Culture Documents
Array Day12
Array Day12
_____________________________________
import java.util.Arrays;
--------------------------------------------------------------------
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.
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};
for(int i=0;i<arr2.length;i++) {
newArr[index]=arr2[i];
index++;
}
}
}
------------------------------------------------------------------------
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();
}
}
}