System arraycopy() method in java

Let’s learn system arraycopy() method in java.

System arraycopy() method in java

System.arraycopy in java copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array.

system arraycopy() method in java

Using arraycopy java a subsequence array components are copied from source array referenced by source to the destination array referenced by destination.

Also read – java two dimensional array program

The components at positions srcPos through srcPos+length-1 in the source array are copied into positions destPos through destPos+length-1, respectively, of the destination array.

System arraycopy in java syntax:

public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)

Parameters:

src: source array to be copied from.

srcPos: starting position in source array.

dest: destination array.

destPos: starting position in destination array.

length: total number of elements to be copied.

Returns:

This method does not return any value.

Throws exception:

IndexOutOfBoundsException – if copying would cause access of data outside array bounds.
ArrayStoreException – if an element in the src array could not be stored into the dest array because of a type mismatch.
NullPointerException – if either src or dest is null.

Here’s the source code for system arraycopy in java implementation.

Implementation:

public class ArrayCopyDemo
{
   public static void main(String[] args)
   {
      int[] arrOne = {2,4,6,8,10,12,14,16,18,20};
      int[] arrTwo = {1,3,5,7,9,11,13,15,17,19};
      int[] sourceArr, sourcePosition, destinationArr[], destinationPosition, len;
      sourceArr = arrOne;
      sourcePosition = 2;
      destinationArr = arrTwo;
      destinationPosition = 5;
      len = 4;
      // printing elements of source array
      System.out.println("Source array : ");
      for(int a = 0; a < arrOne.length; a++)
         System.out.print(arrOne[a] + " ");
      System.out.println("");
      System.out.println("sourcePosition : " + sourcePosition);
      // print elements of destination array
      System.out.println("Destination array : ");
      for(int a = 0; a < arrTwo.length; a++)
         System.out.print(arrTwo[a] + " ");
      System.out.println("");
      System.out.println("destinationPosition : " + destinationPosition);
      System.out.println("Length : " + len);
      // system.arraycopy method in java
      System.arraycopy(sourceArr, sourcePosition, destinationArr, destinationPosition, len);
      // printing final array
      System.out.println("Final array : ");
      for(int a = 0; a < arrTwo.length; a++)
         System.out.print(arrTwo[a] + " ");
   }
}


Output:

Source array : 2 4 6 8 10 12 14 16 18 20
sourcePosition : 2
Destination array : 1 3 5 7 9 11 13 15 17 19
destinationPosition : 5
Length : 4
Final array : 1 3 5 7 9 6 8 10 12 19


java System.arraycopy 2d array

System.arraycopy method is efficient to copy data from one array to another array.

It is even more flexible to copy a portion of source array to destination array.

Meanwhile system.arraycopy method can be used to copy a 2 dimensional array in java. Here’s the code,

import java.util.Arrays;
public class ArrayCopyExample 
{
   public static void main(String[] args) 
   {
      int[][] arrSource = {
              {2, 4, 6, 8}, 
              {1, 3},
              {5, 7, 9, 12, 15}
      };
      int[][] arrDestination = new int[arrSource.length][];	
      for(int a = 0; a < arrSource.length; ++a) 
      {
         arrDestination[a] = new int[arrSource[a].length];
         System.arraycopy(arrSource[a], 0, arrDestination[a], 0, arrDestination[a].length);
      }
      System.out.println("destination array : " + Arrays.deepToString(arrDestination));
   }
}



java.lang.system.arraycopy(native method)

arraycopy method copies elements from source array to destination array. Let’s an example on System.arraycopy().

import java.util.Arrays;
public class ArraycopyNative
{
   public static void main(String[] args) 
   {
      int[] arrSource = { 2, 4, 6, 8, 1, 3, 5, 7, 9 };
      int[] arrDestination = new int[7];
      System.arraycopy(arrSource, 4, arrDestination, 3, 4);
      System.out.println(Arrays.toString(arrDestination));
   }
}


Output:

[0, 0, 0, 1, 3, 5, 7]