目前在Java中數據拷貝提供了如下方式:
clone
System.arraycopy
Arrays.copyOf
Arrays.copyOfRange
下面分別介紹他們的用法
1、clone 方法
clone方法是從Object類繼承過來的,基本數據類型(int ,boolean,char,byte,short,float ,double,long)都可以直接使用clone方法進行克隆,注意String類型是因為其值不可變所以才可以使用。
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]
2、System.arraycopy
System.arraycopy方法是一個本地的方法,源碼里定義如下:
public static native void arraycopy(Object src, int srcPos, Object dest, int desPos, int length)
其參數含義為:
(原數組, 原數組的開始位置, 目標數組, 目標數組的開始位置, 拷貝個數)
1
用法示例
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; }
參數含義:
(原數組,拷貝的個數)
1
用法示例:
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]
1
使用該方法無需我們事先使用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;
參數含義
(原數組,開始位置,拷貝的個數)
1
用法示例:
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]
最后需要注意的是基本類型的拷貝是不影響原數組的值的,如果是引用類型,就不能在這用了,因為數組的拷貝是淺拷貝,對於基本類型可以,對於引用類型是不適合的。
5、那么如何實現對象的深度拷貝呢?
5.1 實現Cloneable接口
實現Cloneable接口,並重寫clone方法,注意一個類不實現這個接口,直接使用clone方法是編譯通不過的。
/**
* Created by Joe on 2018/2/13.
*/
public class Dog implements Cloneable { private String id; private String name; public Dog(String id, String name) { this.id = id; this.name = name; } // 省略 getter 、 setter 以及 toString 方法 @Override public Dog clone() throws CloneNotSupportedException { Dog dog = (Dog) super.clone(); return dog;
}
}
示例:
Dog dog1 = new Dog("1", "Dog1"); Dog dog2 = dog1.clone(); dog2.setName("Dog1 changed"); System.out.println(dog1); // Dog{id='1', name='Dog1'} System.out.println(dog2); // Dog{id='1', name='Dog1 changed'}
5.2 組合類深拷貝
如果一個類里面,又引用其他的類,其他的類又有引用別的類,那么想要深度拷貝必須所有的類及其引用的類都得實現Cloneable接口,重寫clone方法,這樣以來非常麻煩,簡單的方法是讓所有的對象實現序列化接口(Serializable),然后通過序列化反序列化的方法來深度拷貝對象。
public Dog myClone() { Dog dog = null; try { //將對象序列化成為流,因為寫在流是對象里的一個拷貝 //而原始對象扔在存在JVM中,所以利用這個特性可以實現深拷貝 ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream); objectOutputStream.writeObject(this); //將流序列化為對象 ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream); dog = (Dog) objectInputStream.readObject(); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } return dog; }
總結:
本文介紹了關於Java里面的數組拷貝的幾種方式和用法,並給出了如何在Java里面實現對象的深度拷貝,注意除非必需,一般情況下不要使用對象的深度拷貝,因為性能較差。除了自己實現深度拷貝的功能外,網上也有一些開源的工具類也集成了這些功能,如Apache Common Lang3,但原理都大同小異,感興趣的同學可以自己去學習下。
原文鏈接:https://blog.csdn.net/u011669700/article/details/79323251