Although copying an array isn't particularly difficult, it is an
operation which benefits from a native implementation. Therefore
java.lang.System
includes a static
System.arraycopy()
method you can use to copy one array to
another.
public static void arraycopy(Object source, int sourcePosition,
Object destination, int destinationPosition, int numberOfElements)
System.arraycopy()
copies
numberOfElements
elements from the array
source
, beginning with the element at
sourcePosition
, to the array destination
starting at destinationPosition
. The
destination
array must already exist when
System.arraycopy()
is called. The method does not create it.
The source
and destination
arrays must be
of the same type.
For example,
int[] unicode = new int[65536];
for (int i = 0; i < unicode.length; i++) {
unicode[i] = i;
}
int[] latin1 = new int[256];
System.arraycopy(unicode, 0, latin1, 0, 256);