Write a java program in which main creates two int arrays ea
Write a java program in which main creates two int arrays each with 10 slots and then initializes the first array with random integers. main then calls copyArray, passing it the two arrays. copyArray should copy the values from the first array to the second and then return to main. main should then display the contents of both arrays side by side.
Solution
package automativ;
import java.util.*;
public class copyarray {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println(\"enter array limit:\");
int limit=sc.nextInt();
int firstarr[]=new int[limit];
int secondarr[]=new int[limit];
Random rand=new Random();
for(int i=0;i<firstarr.length;i++){
int r=rand.nextInt(10);
firstarr[i]=r;
}
int result[]= copyArray(firstarr,secondarr);
System.out.println(\"first array Array:\");
for(int i=0;i<firstarr.length;i++)
System.out.print(firstarr[i]+\" \");
System.out.println(\"\ \");
System.out.println(\"second Array:\");
for(int i=0;i<result.length;i++)
System.out.print(result[i]+\" \");
}
private static int[] copyArray(int[] firstarr, int[] secondarr) {
secondarr= firstarr.clone();
return secondarr;
}
}
output:
enter array limit:
7
first array Array:
7 5 6 7 6 4 5
second Array:
7 5 6 7 6 4 5
