如何在Java中复制数组

下述方法仅适用于一维数组。 在讨论用Java复制数组的不同方法之前,我们将向您展示如何不复制数组。

如何不在Java中复制数组

Java中的数组是对象。 如果您尝试将它们视为变量…可以(!),但是真正要复制的是引用! 。 下面的示例解释了此语句。

HowNOTtoCopyAnArray.java
package com.mkyong.copyarray;

import java.util.Arrays;

public class HowNOTtoCopyAnArray {
	
	public static void main(String[] args){

		int[] x = {1, 2, 3, 4, 5};
		int[] y = x; //don't copy array like this!
		
		System.out.println(Arrays.toString(x));
		System.out.println(Arrays.toString(y)+"\n");
		
		x[1] = 22; // y[1] will display 22! same reference
		
		System.out.println(Arrays.toString(x));
		System.out.println(Arrays.toString(y)+"\n");
		
		y[4] = 55; // x[4] will display 55!
		
		System.out.println(Arrays.toString(x));
		System.out.println(Arrays.toString(y));

	}

}

输出:

[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]

[1, 22, 3, 4, 5]
[1, 22, 3, 4, 5]

[1, 22, 3, 4, 55]
[1, 22, 3, 4, 55]

1. Object.clone()

ArraysObject类继承方法,而clone是其中之一。 如果您需要照原样复制数组,则应使用此方法。

CloneArray.java
package com.mkyong.copyarray;

import java.util.Arrays;

public class CloneArray {
	
	public static void main(String[] args){

		int[] x = {1, 2, 3, 4, 5};
		int[] y = x.clone();
		
		System.out.println(Arrays.toString(x));
		System.out.println(Arrays.toString(y)+"\n");
		
		x[1] = 22;
		
		System.out.println(Arrays.toString(x));
		System.out.println(Arrays.toString(y)+"\n");
		
		y[4] = 55;
		
		System.out.println(Arrays.toString(x));
		System.out.println(Arrays.toString(y));

	}

}

输出:

[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]

[1, 22, 3, 4, 5]
[1, 2, 3, 4, 5]

[1, 22, 3, 4, 5]
[1, 2, 3, 4, 55]

2. Arrays.copyOf()

Arrays类中,有两种方法可以完全或部分复制数组。 这是copyOf()方法的示例。

ArraysCopyOfMethod.java
package com.mkyong.copyarray;

import java.util.Arrays;

public class ArraysCopyOfMethod {
	
	public static void main(String[] args){

		String[] x = {"one", "two", "three", "four", "five"};
		String[] y = Arrays.copyOf(x, x.length);
		String[] z = Arrays.copyOf(x, 3); //will copy the 3 first elements of array x
		
		System.out.println("Array x: " + Arrays.toString(x));
		System.out.println("Array y: " + Arrays.toString(y));
		System.out.println("Array z: " + Arrays.toString(z));

	}

}

输出:

Array x: [one, two, three, four, five]
Array y: [one, two, three, four, five]
Array z: [one, two, three]

3. Arrays.copyOfRange()

这是copyOfRange()方法的示例。

ArraysCopyOfRangeMethod.java
package com.mkyong.copyarray;

import java.util.Arrays;

public class ArraysCopyOfRangeMethod {
	
	public static void main(String[] args){
		String[] x = {"one", "two", "three", "four", "five"};
		String[] y = Arrays.copyOfRange(x, 0, x.length); //full copy of the array
		String[] z = Arrays.copyOfRange(x, x.length-2, x.length); //copy only the last 2 elements
		
		
		System.out.println("Array x: " + Arrays.toString(x));
		System.out.println("Array y: " + Arrays.toString(y));
		System.out.println("Array z: " + Arrays.toString(z));
	}
}

输出:

Array x: [one, two, three, four, five]
Array y: [one, two, three, four, five]
Array z: [four, five]

4. System.arraycopy()

使用System.arraycopy()您可以控制要复制的源数组中元素的范围,
和预定的位置。

查看System.arraycopy签名( JavaDoc ):

arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
SystemArrayCopy.java
package com.mkyong.copyarray;

import java.util.Arrays;

public class SystemArrayCopy {
	
	public static void main(String[] args){

		String[] x = {"one", "two", "three", "four", "five"};
		String[] y = new String[2];
		System.arraycopy(x, 3, y, 0, 2);
		
		System.out.println("Array x: " + Arrays.toString(x));
		System.out.println("Array y: " + Arrays.toString(y) + "\n");
		
		Object[] z = new Object[5];
		System.arraycopy(x, 0, z, 0, 5);
		System.out.println("Array z: " + Arrays.toString(z)+"\n");
		
		Integer[] w = {3, 4, 5};
		System.out.println("Array w: " + Arrays.toString(w));

		//copy from the second value (1) of array w to z and place in the fourth place (3) the 2 values 
		System.arraycopy(w, 1, z, 3, 2); 
		System.out.println("Array z: " + Arrays.toString(z));

	}
}

输出:

Array x: [one, two, three, four, five]
Array y: [four, five]

Array z: [one, two, three, four, five]

Array w: [3, 4, 5]
Array z: [one, two, three, 4, 5]

注意
别忘了用try catch包围代码,以处理引发的异常

参考文献

  1. 数组JavaDoc
  2. 系统JavaDoc
  3. 如何在Java中复制数组

翻译自: https://mkyong.com/java/how-to-copy-an-array-in-java/

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值