Java 数组复制
给定一个数组,我们需要将其元素复制到另一个数组中,对于一个天真的用户来说,下面的方法是不正确的,如下图所示。
// Java Program to Illustrate Wrong Way Of Copying an Array
// Input array
int a[] = { 1, 8, 3 };
// Creating an array b[] of same size as a[]
int b[] = new int[a.length];
// Doesn't copy elements of a[] to b[], only makes
// b refer to same location
b = a;
输出

输出 解释: 当我们做 “b=a “时,我们实际上是给数组分配了一个引用。因此,如果我们对一个数组做任何改变,也会反映在其他数组中,因为a和b都指向同一个位置。我们也可以用下面的代码来验证它,如下图所示。
例子
// A Java program to demonstrate that simply
// assigning one array reference is incorrect
public class Test {
public static void main(String[] args)
{
int a[] = { 1, 8, 3 };
// Create an array b[] of same size as a[]
int b[] = new int[a.length];
// Doesn't copy elements of a[] to b[],
// only makes b refer to same location
b = a;
// Change to b[] will also reflect in a[]
// as 'a' and 'b' refer to same location.
b[0]++;
System.out.println("Contents of a[] ");
for (int i = 0; i < a.length; i++)
System.out.print(a[i] + " ");
System.out.println("\n\nContents of b[] ");
for (int i = 0; i < b.length; i++)
System.out.print(b[i] + " ");
}
}
输出
Contents of a[]
2 8 3
Contents of b[]
2 8 3
方法
我们已经看到了在复制元素时的内部工作,以及在通过上面产生的错误后需要考虑的边缘情况,所以现在我们可以提出正确的方法来复制数组,如下所示。
- 遍历给定的原始数组的每个元素,每次复制一个元素
- 使用clone()方法
- 使用arraycopy()方法
- 使用Arrays类的copyOf()方法
- 使用Arrays类的copyOfRange()方法
方法1: 对给定的原始数组的每个元素进行迭代,每次复制一个元素。使用这种方法,可以保证对b的任何修改都不会改变原始数组a,如下例所示。
例子
// Java program to demonstrate copying by
// one by one assigning elements between arrays
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Input array a[]
int a[] = { 1, 8, 3 };
// Create an array b[] of same size as a[]
int b[] = new int[a.length];
// Copying elements of a[] to b[]
for (int i = 0; i < a.length; i++)
b[i] = a[i];
// Changing b[] to verify that
// b[] is different from a[]
b[0]++;
// Display message only
System.out.println("Contents of a[] ");
for (int i = 0; i < a.length; i++)
System.out.print(a[i] + " ");
// Display message only
System.out.println("\n\nContents of b[] ");
for (int i = 0; i < b.length; i++)
System.out.print(b[i] + " ");
}
}
输出
Contents of a[]
1 8 3
Contents of b[]
2 8 3
方法2: 使用Clone()方法 ****
在前面的方法中,我们不得不遍历整个数组来做一个副本,我们能不能做得更好?是的,我们可以使用Java中的Clone方法。
例子
// Java program to demonstrate Copying of Array
// using clone() method
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Input array a[]
int a[] = { 1, 8, 3 };
// Copying elements of a[] to b[]
int b[] = a.clone();
// Changing b[] to verify that
// b[] is different from a[]
b[0]++;
// Display message for better readability
System.out.println("Contents of a[] ");
for (int i = 0; i < a.length; i++)
System.out.print(a[i] + " ");
// Display message for better readability
System.out.println("\n\nContents of b[] ");
for (int i = 0; i < b.length; i++)
System.out.print(b[i] + " ");
}
}
输出
Contents of a[]
1 8 3
Contents of b[]
2 8 3
方法3: 使用arraycopy()方法
我们也可以使用 System.arraycopy() 方法。该系统存在于java.lang包中。它的签名是:
public static void arraycopy(Object src, int srcPos, Object dest,
int destPos, int length)
参数
- src 表示源数组。
- srcPos 是复制开始的索引。
- dest 表示目标数组
- destPos 是复制的元素在目标数组中的索引。
- length 是要复制的子数组的长度。
例子
// Java program to demonstrate array
// copy using System.arraycopy()
// Main class
public class GFG {
// Main driver method
public static void main(String[] args)
{
// Custom input array
int a[] = { 1, 8, 3 };
// Creating an array b[] of same size as a[]
int b[] = new int[a.length];
// Copying elements of a[] to b[]
System.arraycopy(a, 0, b, 0, 3);
// Changing b[] to verify that
// b[] is different from a[]
b[0]++;
// Display message only
System.out.println("Contents of a[] ");
for (int i = 0; i < a.length; i++)
System.out.print(a[i] + " ");
// Display message only
System.out.println("\n\nContents of b[] ");
for (int i = 0; i < b.length; i++)
System.out.print(b[i] + " ");
}
}
输出
Contents of a[]
1 8 3
Contents of b[]
2 8 3
方法4: 使用Arrays类的copyOf()方法 ****
如果我们想复制一个数组的前几个元素或数组的完整副本,你可以使用这个方法。
语法
public static int[] copyOf(int[] original, int newLength)
参数
- original原始数组
- newLength要复制的数组的长度。
例子
// Java program to demonstrate array
// copy using Arrays.copyOf()
// Importing Arrays class from utility class
import java.util.Arrays;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Custom input array
int a[] = { 1, 8, 3 };
// Create an array b[] of same size as a[]
// Copy elements of a[] to b[]
int b[] = Arrays.copyOf(a, 3);
// Change b[] to verify that
// b[] is different from a[]
b[0]++;
System.out.println("Contents of a[] ");
// Iterating over array. a[]
for (int i = 0; i < a.length; i++)
System.out.print(a[i] + " ");
System.out.println("\n\nContents of b[] ");
// Iterating over array b[]
for (int i = 0; i < b.length; i++)
System.out.print(b[i] + " ");
}
}
输出
Contents of a[]
1 8 3
Contents of b[]
2 8 3
方法5: 使用数组类的copyOfRange()方法
该方法将指定数组的指定范围复制到一个新的数组中。
public static int[] copyOfRange(int[] original, int from, int to)
参数
- 原有的数组,要从中复制出一个范围
- 要复制的范围的初始索引
- 要复制的范围的最终索引,不包括在内
例子
// Java program to demonstrate array
// copy using Arrays.copyOfRange()
// Importing Arrays class from utility package
import java.util.Arrays;
// Main class
class GFG {
// Main driver method
public static void main(String[] args)
{
// Custom input array
int a[] = { 1, 8, 3, 5, 9, 10 };
// Creating an array b[] and
// copying elements of a[] to b[]
int b[] = Arrays.copyOfRange(a, 2, 6);
// Changing b[] to verify that
// b[] is different from a[]
// Iterating over array a[]
System.out.println("Contents of a[] ");
for (int i = 0; i < a.length; i++)
System.out.print(a[i] + " ");
// Iterating over array b[]
System.out.println("\n\nContents of b[] ");
for (int i = 0; i < b.length; i++)
System.out.print(b[i] + " ");
}
}
输出
Contents of a[]
1 8 3 5 9 10
Contents of b[]
3 5 9 10
最后,让我们确实讨论一下 上述方法的概况 。
- 简单地赋值引用是错误的
- 数组可以通过遍历数组,一个一个地赋值元素来进行复制。
- 我们可以 使用clone()或System.arraycopy()来 避免元素的迭代。
- clone()可以创建一个相同大小的新数组,但是 System.arraycopy() 可以用来从源范围复制到目标范围。
- System.arraycopy()比clone()快,因为它使用了Java本地接口。
- 如果你想复制一个数组的前几个元素或一个数组的完整副本,你可以使用Arrays.copyOf()方法。
- Arrays.copyOfRange()是用来复制一个数组的指定范围。如果起始索引不是0,你可以用这个方法来复制一个部分数组。
极客教程