Wednesday, May 18, 2011

Copying arrays in java

  • ArrayCopy
    System.arraycopy(from, fromIndex, to, toIndex, count);

    eg. System.arraycopy(arr1, 2, arr2, 3, 4);
    The shallowcopy starts at position 2 in the source array i.e. arr1 and copies four entries, starting at position 3 of the target array i.e. arr
  • CopyOf

    arr2 = Arrays.copyOf(a, newLength);

    New shallow copy of array with new length. If longer, uses default values.
  • copyOfRange

    arr2 = Arrays.copyOfRange(a, from, to);

    New shallow copy of portion of array.
  • No comments:

    Post a Comment

    Chitika