Java System Arraycopy Example

Java system arraycopy example gives us the insight of how array copy works in java.

Since array topic is large in its own context we will see one of its example below,

Java system arraycopy example

public class Array_Copy {

public static void main(String[] args) {

int arr1[] = {2,4,6,8,10,12,14,16,18,20};
int arr2[] = {1,3,5,7,9,11,13,15,17,19};

System.out.println("First Array: ");
for(int i = 0;i < 10;i++){
System.out.println(arr1[i] + "");
}

System.out.println("\n Second Array: ");
for(int i = 0;i < 10;i++){
System.out.println(arr2[i] + "");
}

System.arraycopy(arr1, 4, arr2, 4, 5);

// Here source array is arr1
// source position is 4
// source destination array is arr2
// destination position is 4
// length is 5

System.out.println("\n After calling arraycopy() the second array is: ");
for(int i = 0;i < 10;i++){
System.out.println(arr2[i] + "");
}
}
}

Output:

Java system arraycopy example

As you can see in the above example, System.arraycopy method is used. The method syntax and parameters are,

public static void arraycopy(Object source, int sourcePosition, Object destination, int destinationPosition, int length)

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

A sub sequence of array components are copied from the source array referenced by source to the destination array referenced by destination.

The number of components copied is equal to the length argument. The components at positions sourcePosition through sourcePosition+length-1 in the source array are copied into positions destinationPosition through destinationPosition+length-1, respectively, of the destination array.

Also Read – Calculate Average Of An Array In Java

If the source and destination arguments refer to the same array object, then the copying is performed as if the components at positions sourcePosition through sourcePosition+length-1 were first copied to a temporary array with length components and then the contents of the temporary array were copied into positions destinationPosition through destinationPosition+length-1 of the destination array.If destination is null, then a NullPointerException is thrown.If source is null, then a NullPointerException is thrown and the destination array is not modified.

Otherwise, if any of the following is true, an ArrayStoreException is thrown and the destination is not modified:

The source argument refers to an object that is not an array.
The destination argument refers to an object that is not an array.
The source argument and destination argument refer to arrays whose component types are different primitive types.
The source argument refers to an array with a primitive component type and the destination argument refers to an array with a reference component type.
The source argument refers to an array with a reference component type and the destination argument refers to an array with a primitive component type.

Otherwise, if any of the following is true, an IndexOutOfBoundsException is thrown and the destination is not modified:The source_Position argument is negative.
The destinationPosition argument is negative.
The length argument is negative.
sourcePosition+length is greater than source.length, the length of the source array.
destinationPosition+length is greater than destination.length, the length of the destination array.

Otherwise, if any actual component of the source array from position sourcePosition through sourcePosition+length-1 cannot be converted to the component type of the destination array by assignment conversion, an ArrayStoreException is thrown.

In this case, let k be the smallest nonnegative integer less than length such that source[sourcePosition+k] cannot be converted to the component type of the destination array; when the exception is thrown, source array components from positions sourcePosition through sourcePosition+k-1 will already have been copied to destination array positions destinationPosition through destinationPosition+k-1 and no other positions of the destination array will have been modified. (Because of the restrictions already itemized, this paragraph effectively applies only to the situation where both arrays have component types that are reference types.)

Parameters:

  • source – the source array.
  • sourcePosition – starting position in the source array.
  • destination – the destination array.
  • destinationPosition – starting position in the destination data.
  • length – the number of array elements to be copied.

Exceptions:

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

conclusion

So this is all about java system arraycopy example. I hope you have understood the concept of arraycopy in java.

You can subscribe to my blog flower brackets if you haven’t already.

About the author

admin

View all posts

Leave a Reply

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