суббота, 24 сентября 2016 г.

What is more efficient: System.arraycopy vs Arrays.copyOf?

The difference is that Arrays.copyOf does not only copy elements, it also creates a new array. System.arrayCopy copies into an existing array.
Here is the source for Arrays.copyOf, as you can see it uses System.arraycopy internally to fill up the new array.
  public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
    T[] copy = ((Object)newType == (Object)Object[].class)
        ? (T[]) new Object[newLength]
        : (T[]) Array.newInstance(newType.getComponentType(), newLength);
    System.arraycopy(original, 0, copy, 0,
                     Math.min(original.length, newLength));
    return copy;

Комментариев нет:

Отправить комментарий