System arraycopy() method in java

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

System arraycopy() method in java

System Arraycopy method copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array.

Using arraycopy() method subsequence array components are copied from source array referenced by source to the destination array referenced by destination.

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.

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 example on system arraycopy method in java.

Example:

public class SystemArraycopyMethodExample
{
   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


System arraycopy() method 2d array

System.arraycopy() method can be used efficiently 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 for java arraycopy 2d array.

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));
   }
}

System.arraycopy(native method)

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

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]


System arraycopy() method FAQ’s

In which class do I find ArrayCopy method in java?

java.lang.System class.

Is System.arraycopy deep copy?

No. It does a shallow copy.

Does System.arraycopy method create a new array?

No. System.arraycopy() method simply copies values from the source array to the destination array. Before copying, the Java Virtual Machine checks if the both source and destination types are the same.

Is System.arraycopy() method faster than for loop?

Yes faster than for loop.

What does system Arraycopy method do in Java?

System Arraycopy method copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array.

Reference – oracle docs

Also read – major features of java