How to effectively copy an array in java ?
Posted
by Tony
on Stack Overflow
See other posts from Stack Overflow
or by Tony
Published on 2010-04-07T03:00:22Z
Indexed on
2010/04/07
3:03 UTC
Read the original article
Hit count: 345
java
|efficiency
The toArray method in ArrayList , Bloch uses both System.arraycopy and Arrays.copyOf to copy an array .
public <T> T[] toArray(T[] a) {
if (a.length < size)
// Make a new array of a's runtime type, but my contents:
return (T[]) Arrays.copyOf(elementData, size, a.getClass());
System.arraycopy(elementData, 0, a, 0, size);
if (a.length > size)
a[size] = null;
return a;
}
How to compare these two copy method , when to use which ? Thanks.
© Stack Overflow or respective owner