文章目录

  • 1、clone 方法
  • 2、System.arraycopy
  • 3、 Arrays.copyOf
  • 4、 Arrays.copyOfRange



Java中数据拷贝提供了如下几种方式:

  • clone
  • System.arraycopy
  • Arrays.copyOf
  • Arrays.copyOfRange
  • for循环暴力复制
    效率:System.arraycopy > clone > Arrays.copyOf /Arrays.copyOfRange>暴力复制 四种方式都是浅拷贝,解释一下浅拷贝,如果属性是基本类型,拷贝的就是基本类型的值;如果属性是内存地址(引用类型),拷贝的就是内存地址 ,因此如果其中一个对象改变了这个地址指向的内容,就会影响到另一个对象,但是如果其中一个对象的该属性不再指向原地址,而是指向了别的地址,则另一个对象不受影响。
  • java 实现 零拷贝 java数据拷贝_数组


1、clone 方法

int 类型示例

int[] a1 = {1, 3};
int[] a2 = a1.clone();
a1[0] = 666;
System.out.println(Arrays.toString(a1));   //[666, 3]
System.out.println(Arrays.toString(a2));   //[1, 3]

String类型示例

String[] a1 = {"a1", "a2"};
String[] a2 = a1.clone();
a1[0] = "b1"; //更改a1数组中元素的值
System.out.println(Arrays.toString(a1));   //[b1, a2]
System.out.println(Arrays.toString(a2));   //[a1, a2]

基本类型二维数组的clone:
对于二维数组a = {{1,2},{3,4},{5,6}},其实是a={a[0],a[1],a[2]},其中一维数组a[0],a[1],a[2]都是引用类型的对象,所以直接clone会复制一维数组的地址,克隆数组和原数组的a[0],a[1],a[2]b[0],b[1],b[2]指向相同的地址,那么其中一个二维数组的某个一维数组内容发生改变,另一个二维数组也会发生改变;但是如果其中一个二维数组有某个一维数组(引用类型的对象)指向了别的数组,则另一个二维数组不会发生改变

int[][] a = {{1, 2}, {3, 4}, {5, 6}};
int[][] b = a.clone();
b[0][0] = -1;
System.out.println("a:");
for (int i = 0; i < a.length; ++i) {
    System.out.println(Arrays.toString(a[i]));
}
System.out.println("b:");
for (int i = 0; i < b.length; ++i) {
    System.out.println(Arrays.toString(b[i]));
}
int[] c = {6, 6};
//a[1]指向别的数组
a[1] = c;
System.out.println("a:");
for (int i = 0; i < a.length; ++i) {
    System.out.println(Arrays.toString(a[i]));
}
System.out.println("b:");
for (int i = 0; i < b.length; ++i) {
    System.out.println(Arrays.toString(b[i]));
}
// 输出:
a:
[-1, 2]
[3, 4]
[5, 6]
b:
[-1, 2]
[3, 4]
[5, 6]
a:
[-1, 2]
[6, 6]
[5, 6]
b:
[-1, 2]
[3, 4]
[5, 6]

所以基本类型二维数组不应该整体clone,而应该对一维数组进行克隆

int[][] a = {{1, 2}, {3, 4}, {5, 6}};
int[][] b = new int[3][2];
for(int i = 0; i < a.length; ++i){
    b[i] = a[i].clone();
}
b[0][0] = -1;
System.out.println("a:");
for (int i = 0; i < a.length; ++i) {
    System.out.println(Arrays.toString(a[i]));
}
System.out.println("b:");
for (int i = 0; i < b.length; ++i) {
    System.out.println(Arrays.toString(b[i]));
}
//输出
a:
[1, 2]
[3, 4]
[5, 6]
b:
[-1, 2]
[3, 4]
[5, 6]

2、System.arraycopy

System.arraycopy方法是一个本地的方法,源码里定义如下:

public static native void arraycopy(Object src, int srcPos, Object dest, int desPos, int length)

其参数含义为:

(原数组, 原数组的开始位置, 目标数组, 目标数组的开始位置, 拷贝个数)

用法示例

int[] a1 = {1, 2, 3, 4, 5};
int[] a2 = new int[10];
System.arraycopy(a1, 1, a2, 3, 3);
System.out.println(Arrays.toString(a1)); // [1, 2, 3, 4, 5]
System.out.println(Arrays.toString(a2)); // [0, 0, 0, 2, 3, 4, 0, 0, 0, 0]

当使用这个方法的时候,需要复制到一个已经分配内存单元的数组。

3、 Arrays.copyOf

Arrays.copyOf底层其实也是用的System.arraycopy 源码如下:

public static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
    @SuppressWarnings("unchecked")
    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;
}

参数含义:

(原数组,拷贝的个数)

用法示例:

int[] a1 = {1, 2, 3, 4, 5};
int[] a2 = Arrays.copyOf(a1, 3);
System.out.println(Arrays.toString(a1)) // [1, 2, 3, 4, 5]
System.out.println(Arrays.toString(a2)) // [1, 2, 3]

使用该方法无需我们事先使用new关键字对对象进行内存单元的分配

4、 Arrays.copyOfRange

Arrays.copyOfRange底层其实也是用的System.arraycopy,只不过封装了一个方法

public static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) {
    int newLength = to - from;
    if (newLength < 0)
        throw new IllegalArgumentException(from + " > " + to);
    @SuppressWarnings("unchecked")
    T[] copy = ((Object)newType == (Object)Object[].class)
        ? (T[]) new Object[newLength]
        : (T[]) Array.newInstance(newType.getComponentType(), newLength);
    System.arraycopy(original, from, copy, 0,
                     Math.min(original.length - from, newLength));
    return copy;
}

参数含义

(原数组,开始位置,拷贝的个数)

用法示例:

int[] a1 = {1, 2, 3, 4, 5};
int[] a2 = Arrays.copyOfRange(a1, 0, 1);
System.out.println(Arrays.toString(a1)) // [1, 2, 3, 4, 5]
System.out.println(Arrays.toString(a2)) // [1]

最后需要注意的是基本类型的拷贝是不影响原数组的值的,如果是引用类型,就不能在这用了,因为数组的拷贝是浅拷贝,对于基本类型可以,对于引用类型是不适合的,引用类型只是复制了引用的地址,原数组引用对象的属性改变会影响拷贝数组。