Copying an array to another array using System.arraycopy() method

  The method System.arraycopy() present in System class is used for copying one array into another array.
  Parameters: System.arraycopy(sourceArray,sourceArrayStartPos,destArray,destArrayStartPos,numberOfElements)

Example.

public class CopyArray {
    public static void main(String[] args) {
        try{
            int array1[] = {1,3,4,5,6,7,8};
            int array2[] = new int[7];
            System.arraycopy(array1, 0, array2, 0, 7);
            for(int i=0;i<array2.length;i++) {
                System.out.println(array2[i]);
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Comments

Popular Posts