我有一个不断更新的数组a。我们设a =[1,2,3,4,5]。我需要做一个完全相同的副本a,并称之为b。如果a改变为[6,7,8,9,10],b应该仍然是[1,2,3,4,5]。最好的方法是什么?我尝试了一个for循环:
for(int i=0; i<5; i++) {
b[i]=a[i];
}
但这似乎并不正确。请不要使用像深度复制等高级术语,因为我不知道那是什么意思。
我有一个不断更新的数组a。我们设a =[1,2,3,4,5]。我需要做一个完全相同的副本a,并称之为b。如果a改变为[6,7,8,9,10],b应该仍然是[1,2,3,4,5]。最好的方法是什么?我尝试了一个for循环:
for(int i=0; i<5; i++) {
b[i]=a[i];
}
但这似乎并不正确。请不要使用像深度复制等高级术语,因为我不知道那是什么意思。
当前回答
如果你想复制下列文件:
int[] a = {1,2,3,4,5};
这是一条正确的道路:
int[] b = Arrays.copyOf(a, a.length);
数组。在小型数组上,copyOf可能比a.r onclone()更快。两者复制元素的速度相同,但clone()返回Object,因此编译器必须插入隐式转换为int[]。你可以在字节码中看到它,就像这样:
ALOAD 1
INVOKEVIRTUAL [I.clone ()Ljava/lang/Object;
CHECKCAST [I
ASTORE 2
其他回答
你也可以使用Arrays.copyOfRange。
例子:
public static void main(String[] args) {
int[] a = {1,2,3};
int[] b = Arrays.copyOfRange(a, 0, a.length);
a[0] = 5;
System.out.println(Arrays.toString(a)); // [5,2,3]
System.out.println(Arrays.toString(b)); // [1,2,3]
}
此方法类似于Arrays。copyOf,但是它更灵活。它们都使用System。Arraycopy在引擎盖下。
See:
https://docs.oracle.com/javase/8/docs/api/java/util/Arrays.html https://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/8u40-b25/java/util/Arrays.java?av=f
您可以尝试在Java中使用Arrays.copyOf()
int[] a = new int[5]{1,2,3,4,5};
int[] b = Arrays.copyOf(a, a.length);
如果你必须使用原始数组而不是ArrayList,那么arrays可以满足你的需要。如果查看源代码,这些绝对是获取数组副本的最佳方法。它们确实有一些很好的防御性编程,因为如果您向System.arraycopy()方法提供不合逻辑的参数,它会抛出许多未检查的异常。
您可以使用Arrays.copyOf(),它将从第一个到第n个元素复制到新的较短数组。
public static <T> T[] copyOf(T[] original, int newLength)
Copies the specified array, truncating or padding with nulls (if necessary) so the copy has the specified length. For all indices that are valid in both the original array and the copy, the two arrays will contain identical values. For any indices that are valid in the copy but not the original, the copy will contain null. Such indices will exist if and only if the specified length is greater than that of the original array. The resulting array is of exactly the same class as the original array.
2770
2771 public static <T,U> T[] More ...copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
2772 T[] copy = ((Object)newType == (Object)Object[].class)
2773 ? (T[]) new Object[newLength]
2774 : (T[]) Array.newInstance(newType.getComponentType(), newLength);
2775 System.arraycopy(original, 0, copy, 0,
2776 Math.min(original.length, newLength));
2777 return copy;
2778 }
或者Arrays.copyOfRange()也可以做到这一点:
public static <T> T[] copyOfRange(T[] original, int from, int to)
Copies the specified range of the specified array into a new array. The initial index of the range (from) must lie between zero and original.length, inclusive. The value at original[from] is placed into the initial element of the copy (unless from == original.length or from == to). Values from subsequent elements in the original array are placed into subsequent elements in the copy. The final index of the range (to), which must be greater than or equal to from, may be greater than original.length, in which case null is placed in all elements of the copy whose index is greater than or equal to original.length - from. The length of the returned array will be to - from. The resulting array is of exactly the same class as the original array.
3035 public static <T,U> T[] More ...copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) {
3036 int newLength = to - from;
3037 if (newLength < 0)
3038 throw new IllegalArgumentException(from + " > " + to);
3039 T[] copy = ((Object)newType == (Object)Object[].class)
3040 ? (T[]) new Object[newLength]
3041 : (T[]) Array.newInstance(newType.getComponentType(), newLength);
3042 System.arraycopy(original, from, copy, 0,
3043 Math.min(original.length - from, newLength));
3044 return copy;
3045 }
如您所见,这两个都是系统的包装器函数。Arraycopy的防御逻辑,你试图做的是有效的。
系统。Arraycopy绝对是复制数组最快的方法。
您可以尝试使用System.arraycopy()
int[] src = new int[]{1,2,3,4,5};
int[] dest = new int[5];
System.arraycopy( src, 0, dest, 0, src.length );
但是,在大多数情况下使用clone()可能更好:
int[] src = ...
int[] dest = src.clone();
你可以使用
int[] a = new int[]{1,2,3,4,5};
int[] b = a.clone();
也