Public classarraycopy{ Public Static voidMain (String []args) {int[]a = {1,3,4,5}; Toprint (a); int[]afor=New int[A.length]; //1.for Cyclic replicationSystem.out.println ("===========1. Using for Replication"); for(inti=0;i<a.length;i++) {Afor[i]=A[i]; } afor[2]=10;//Change the value in the original array of values in the Afor unchangedSystem.out.print ("Array A:"); Toprint (a); System.out.print ("Array afor:"); Toprint (afor); //**2. Using System.arraycopy (src,srcpos,dest,destpos,length);System.out.println ("===========2. Use system.arraycopy copy \ n Copy afor to a:"); System.arraycopy (Afor,0,a,0, a.length); afor[1]=9;//change the value of AforToprint (a); Toprint (afor); //3. Copy using CloneSystem.out.println ("===========3. Using clone to copy the value of Afor to a"); A=(int[]) Afor.clone (); afor[0]=8; Toprint (a); Toprint (afor); //4. Using copyof and Copyofrange of the arrays class for array replicationSystem.out.println ("===========4. Use arrays.copyof/to copy the value of Afor to a"); A=java.util.arrays.copyof (afor,afor.length+1); afor[3]=11; Toprint (a); Toprint (afor); } Static voidToprint (int[] a) { for(intaa:a) {System.out.print (" "+AA); } System.out.println (); }}
Copy of the Java array Four methods: For, clone, System.arraycopy, arrays.copyof