System.arraycopy(sourceArray,
sourceStartIndex,
targetArray,
targetStartIndex,
length);
Example,
String[] source = { "alpha", "beta", "gamma" };
String[] target = new String[source.length];
System.arraycopy(source, 0, target, 0, source.length);
or use Arrays.copyOf()
Example,
target = Arrays.copyOf(source, length);
java.util.Arrays.copyOf(byte[] source, int length)
was added in JDK 1.6.
The copyOf()
method uses System.arrayCopy()
to make a copy of the array, but is more flexible than clone()
since you can make copies of parts of an array.