Arrays.copyOf()用于复制数组内容,可实现扩容。该方法对不同数据类型有相应的重载方法,详见源码。
    copyOf(T[] original, int newLength):

    1. /**
    2. * Copies the specified array, truncating or padding with nulls (if necessary)
    3. * so the copy has the specified length. For all indices that are
    4. * valid in both the original array and the copy, the two arrays will
    5. * contain identical values. For any indices that are valid in the
    6. * copy but not the original, the copy will contain <tt>null</tt>.
    7. * Such indices will exist if and only if the specified length
    8. * is greater than that of the original array.
    9. * The resulting array is of exactly the same class as the original array.
    10. *
    11. * @param <T> the class of the objects in the array
    12. * @param original the array to be copied
    13. * @param newLength the length of the copy to be returned
    14. * @return a copy of the original array, truncated or padded with nulls
    15. * to obtain the specified length
    16. * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
    17. * @throws NullPointerException if <tt>original</tt> is null
    18. * @since 1.6
    19. */
    20. @SuppressWarnings("unchecked")
    21. public static <T> T[] copyOf(T[] original, int newLength) {
    22. return (T[]) copyOf(original, newLength, original.getClass());
    23. }

    original为要复制的数组,newLength为要复制的长度,默认从0开始复制。复制长度大于原数组长度,使用默认值填充,反之复制到第length个元素(length-1)即可。

    T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType):

    1. /**
    2. * Copies the specified array, truncating or padding with nulls (if necessary)
    3. * so the copy has the specified length. For all indices that are
    4. * valid in both the original array and the copy, the two arrays will
    5. * contain identical values. For any indices that are valid in the
    6. * copy but not the original, the copy will contain <tt>null</tt>.
    7. * Such indices will exist if and only if the specified length
    8. * is greater than that of the original array.
    9. * The resulting array is of the class <tt>newType</tt>.
    10. *
    11. * @param <U> the class of the objects in the original array
    12. * @param <T> the class of the objects in the returned array
    13. * @param original the array to be copied
    14. * @param newLength the length of the copy to be returned
    15. * @param newType the class of the copy to be returned
    16. * @return a copy of the original array, truncated or padded with nulls
    17. * to obtain the specified length
    18. * @throws NegativeArraySizeException if <tt>newLength</tt> is negative
    19. * @throws NullPointerException if <tt>original</tt> is null
    20. * @throws ArrayStoreException if an element copied from
    21. * <tt>original</tt> is not of a runtime type that can be stored in
    22. * an array of class <tt>newType</tt>
    23. * @since 1.6
    24. */
    25. public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
    26. @SuppressWarnings("unchecked")
    27. T[] copy = ((Object)newType == (Object)Object[].class)
    28. ? (T[]) new Object[newLength]
    29. : (T[]) Array.newInstance(newType.getComponentType(), newLength);
    30. System.arraycopy(original, 0, copy, 0,
    31. Math.min(original.length, newLength));
    32. return copy;
    33. }

    代码拷贝调用的是System.arraycopy()方法,返回新数组对象,不会影响原数组。

    System.arraycopy():

    1. public static native void arraycopy(Object src, int srcPos,
    2. Object dest, int destPos,
    3. int length);

    src:源对象
    srcPos:源数组中的起始位置
    dest:目标数组对象
    destPos:目标数据中的起始位置
    length:要拷贝的数组元素的数量

    Java实现数组复制的4种方法:
    1.Arrays.copyOf()
    2.Arrays.copyOfRange()
    3.System.arraycopy()
    4.Object.clone()

    Arrays.copyOfRange():

    1. /**
    2. * Copies the specified range of the specified array into a new array.
    3. * The initial index of the range (<tt>from</tt>) must lie between zero
    4. * and <tt>original.length</tt>, inclusive. The value at
    5. * <tt>original[from]</tt> is placed into the initial element of the copy
    6. * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
    7. * Values from subsequent elements in the original array are placed into
    8. * subsequent elements in the copy. The final index of the range
    9. * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
    10. * may be greater than <tt>original.length</tt>, in which case
    11. * <tt>null</tt> is placed in all elements of the copy whose index is
    12. * greater than or equal to <tt>original.length - from</tt>. The length
    13. * of the returned array will be <tt>to - from</tt>.
    14. * <p>
    15. * The resulting array is of exactly the same class as the original array.
    16. *
    17. * @param <T> the class of the objects in the array
    18. * @param original the array from which a range is to be copied
    19. * @param from the initial index of the range to be copied, inclusive
    20. * @param to the final index of the range to be copied, exclusive.
    21. * (This index may lie outside the array.)
    22. * @return a new array containing the specified range from the original array,
    23. * truncated or padded with nulls to obtain the required length
    24. * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
    25. * or {@code from > original.length}
    26. * @throws IllegalArgumentException if <tt>from &gt; to</tt>
    27. * @throws NullPointerException if <tt>original</tt> is null
    28. * @since 1.6
    29. */
    30. @SuppressWarnings("unchecked")
    31. public static <T> T[] copyOfRange(T[] original, int from, int to) {
    32. return copyOfRange(original, from, to, (Class<? extends T[]>) original.getClass());
    33. }
    34. /**
    35. * Copies the specified range of the specified array into a new array.
    36. * The initial index of the range (<tt>from</tt>) must lie between zero
    37. * and <tt>original.length</tt>, inclusive. The value at
    38. * <tt>original[from]</tt> is placed into the initial element of the copy
    39. * (unless <tt>from == original.length</tt> or <tt>from == to</tt>).
    40. * Values from subsequent elements in the original array are placed into
    41. * subsequent elements in the copy. The final index of the range
    42. * (<tt>to</tt>), which must be greater than or equal to <tt>from</tt>,
    43. * may be greater than <tt>original.length</tt>, in which case
    44. * <tt>null</tt> is placed in all elements of the copy whose index is
    45. * greater than or equal to <tt>original.length - from</tt>. The length
    46. * of the returned array will be <tt>to - from</tt>.
    47. * The resulting array is of the class <tt>newType</tt>.
    48. *
    49. * @param <U> the class of the objects in the original array
    50. * @param <T> the class of the objects in the returned array
    51. * @param original the array from which a range is to be copied
    52. * @param from the initial index of the range to be copied, inclusive
    53. * @param to the final index of the range to be copied, exclusive.
    54. * (This index may lie outside the array.)
    55. * @param newType the class of the copy to be returned
    56. * @return a new array containing the specified range from the original array,
    57. * truncated or padded with nulls to obtain the required length
    58. * @throws ArrayIndexOutOfBoundsException if {@code from < 0}
    59. * or {@code from > original.length}
    60. * @throws IllegalArgumentException if <tt>from &gt; to</tt>
    61. * @throws NullPointerException if <tt>original</tt> is null
    62. * @throws ArrayStoreException if an element copied from
    63. * <tt>original</tt> is not of a runtime type that can be stored in
    64. * an array of class <tt>newType</tt>.
    65. * @since 1.6
    66. */
    67. public static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) {
    68. int newLength = to - from;
    69. if (newLength < 0)
    70. throw new IllegalArgumentException(from + " > " + to);
    71. @SuppressWarnings("unchecked")
    72. T[] copy = ((Object)newType == (Object)Object[].class)
    73. ? (T[]) new Object[newLength]
    74. : (T[]) Array.newInstance(newType.getComponentType(), newLength);
    75. System.arraycopy(original, from, copy, 0,
    76. Math.min(original.length - from, newLength));
    77. return copy;
    78. }

    original表示原数组。
    from表示开始复制的起始索引,目标数组中将包含起始索引对应的元素,另外,from必须在 0 到 original.length 之间。
    to表示终止索引,目标数组中将不包含终止索引对应的元素,to必须大于等于 from,可以大于 original.length,如果大于 original.length,则目标数组中使用默认值填充。

    Object.clone():

    1. int[] targetArray=(int[])sourceArray.clone();

    以上方法都是浅拷贝(浅复制)。浅拷贝只是复制了对象的引用地址,两个对象指向同一个内存地址,所以修改其中任意的值,另一个值都会随之变化。深拷贝是将对象及值复制过来,两个对象修改其中任意的值另一个值不会改变。