The
java.lang.System.arraycopy() method copies elements from the specified source array, beginning at the specified position, to the specified position of the destination array:public static void arraycopy(Object source, int sourcePosition,
source : Array from which elements are to be copied.
sourcePosition : Index that specifies the starting point in the range of elements to copy from the array. This value can be any valid array index. destination : Specifies the destination array that will store the copy. destinationPosition : Specifies the index in the destination array where the first copied element should be stored. numberOfElements : Specifies the number of elements to copy from the array in the first argument. NOTE: The elements at positions sourcePosition to (sourcePosition + numberOfElements- 1)are copied into positions destinationPosition to (destinationPosition + numberOfElements – 1), respectively, of the destination array. Exceptions thrown by arraycopy() :
The following example shows the usage of arraycopy() method: ArrayCopyDemo1 CODE class ArrayCopyDemo1 OUTPUT Java DESCRIPTION THINGS TO TRY
For Example : System.arraycopy(charArray, 4, charArrayCopy, 0, 4) copies characters at positions 4 through 7
from the array to the destination array, arrayCopy. Remember that the indices we pass to the arraycopy() must be valid. Otherwise, an Exception will be thrown.ArrayCopyDemo2 CODE class ArrayCopyDemo2 OUTPUT 5 1 4 9 DESCRIPTION THINGS TO TRY
|