Java System.arraycopy() Method Examples

Filed Under: Java
System.arraycopy()

Java System.arraycopy() is a native static method to copy elements from the source array to the destination array.

System.arraycopy() Method Arguments

The System.arraycopy() method signature is:


public static native void arraycopy(Object src,  int  srcPos,
     Object dest, int destPos,
     int length);
  • src: the source array.
  • srcPos: source array index from where copy will start
  • dest: the destination array.
  • destPos: destination array index from where data will be copied.
  • length: the number of elements to copy.

Java System.arraycopy() Example

Let’s look at a simple example to copy array using this method.


package com.journaldev.examples;

import java.util.Arrays;

public class SystemArrayCopy {

	public static void main(String[] args) {
		int[] source = { 1, 2, 3, 4, 5, 6, 7 };
		int[] destination = new int[5];

		System.arraycopy(source, 3, destination, 2, 3);

		System.out.println(Arrays.toString(destination));
	}
}

Output

Java System.arraycopy Example

Java System.arraycopy() Example

The elements from the source array to copy are 4,5,6. These are copied to the destination array starting from index 2.

System.arraycopy() when source and destination are same array

When the source and destination are the same arrays, a temporary array is created from the source array. Then the elements from the temporary array are copied to the source array.


jshell> int[] source = { 1, 2, 3, 4, 5, 6, 7 };
source ==> int[7] { 1, 2, 3, 4, 5, 6, 7 }

jshell> System.arraycopy(source, 3, source, 5, 2);

jshell> System.out.println(Arrays.toString(source));
[1, 2, 3, 4, 5, 4, 5]

Exception Scenarios with System arraycopy() Method

The arraycopy() method has 5 arguments. So, there are many instances when an exception can occur. The exceptions thrown by this method are:

  • NullPointerException: if source or destination array is null.
  • ArrayStoreException: if the source and destination array type doesn’t match or they are not array.
  • ArrayIndexOutOfBoundsException: if the data overflow occurs because of index values or they are negative.

Let’s look at some examples of these exception scenarios.

1. NullPointerException Examples


jshell> System.arraycopy(source, 3, null, 2, 3);
|  Exception java.lang.NullPointerException
|        at System.arraycopy (Native Method)
|        at (#4:1)

jshell> System.arraycopy(null, 3, source, 2, 3);
|  Exception java.lang.NullPointerException
|        at System.arraycopy (Native Method)
|        at (#5:1)

2. ArrayStoreException when source and destination are not an array


jshell> System.arraycopy(new Object(), 3, source, 2, 3);
|  Exception java.lang.ArrayStoreException: arraycopy: source type java.lang.Object is not an array
|        at System.arraycopy (Native Method)
|        at (#6:1)

jshell> System.arraycopy(source, 3, "ABC", 2, 3);
|  Exception java.lang.ArrayStoreException: arraycopy: destination type java.lang.String is not an array
|        at System.arraycopy (Native Method)
|        at (#7:1)

3. ArrayStoreException when source and destination arrays are not compatible


jshell> int[] source = {1, 2, 3}
source ==> int[3] { 1, 2, 3 }

jshell> String[] destination = new String[5];
destination ==> String[5] { null, null, null, null, null }

jshell> System.arraycopy(source, 0, destination, 0, 2);
|  Exception java.lang.ArrayStoreException: arraycopy: type mismatch: can not copy int[] into object array[]
|        at System.arraycopy (Native Method)
|        at (#10:1)

4. ArrayIndexOutOfBoundsException when index or length is negative


jshell> int[] source = {1, 2, 3}
source ==> int[3] { 1, 2, 3 }

jshell> int[] dest = new int[5];
dest ==> int[5] { 0, 0, 0, 0, 0 }

jshell> System.arraycopy(source, -1, dest, 0, 2);
|  Exception java.lang.ArrayIndexOutOfBoundsException: arraycopy: source index -1 out of bounds for int[3]
|        at System.arraycopy (Native Method)
|        at (#15:1)

jshell> System.arraycopy(source, 1, dest, -1, 2);
|  Exception java.lang.ArrayIndexOutOfBoundsException: arraycopy: destination index -1 out of bounds for int[5]
|        at System.arraycopy (Native Method)
|        at (#16:1)

jshell> System.arraycopy(source, 1, dest, 0, -2);
|  Exception java.lang.ArrayIndexOutOfBoundsException: arraycopy: length -2 is negative
|        at System.arraycopy (Native Method)
|        at (#17:1) 

5. ArrayIndexOutOfBoundsException when srcPos+length is greater than src.length


jshell> System.arraycopy(source, 0, dest, 0, 4);
|  Exception java.lang.ArrayIndexOutOfBoundsException: arraycopy: last source index 4 out of bounds for int[3]
|        at System.arraycopy (Native Method)
|        at (#18:1)

6. ArrayIndexOutOfBoundsException when destPos+length is greater than dest.length


jshell> int[] src = {1, 2, 3, 4}
src ==> int[4] { 1, 2, 3, 4 }

jshell> int[] dest = new int[2]
dest ==> int[2] { 0, 0 }

jshell> System.arraycopy(src, 0, dest, 0, 3);
|  Exception java.lang.ArrayIndexOutOfBoundsException: arraycopy: last destination index 3 out of bounds for int[2]
|        at System.arraycopy (Native Method)
|        at (#21:1)

Conclusion

Java System arraycopy() native method can be used to copy array elements. However, there are many scenarios which can lead to runtime exceptions. So you should use it carefully when you know the length of the source and destination array.

References

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages