java代码如何实现数组复制呢?

乔欣 Java经验 发布时间:2023-01-30 23:13:36 阅读数:3814 1
下文笔者讲述数组复制的方法分享,如下所示
数组复制实现思路:
     方式1:
	   Object.clone()
	 方式2:
	   Arrays.copyOf()
	 方式3:
	   Arrays.copyOfRange()
	 方式4:
	   System.arraycopy()

数组复制注意事项:
    不能采用赋值的方式实现数组复制
	错误的写法:
	    int[] x = {88,99,110};
		int[] y = x; //不能实现数组复制

Object.clone()

由于Arrays从Object类继承方法
   Object中拥有clone方法
   所以我们可以采用此方法复制数组
package com.java265.copyarray;
 
import java.util.Arrays;
 
public class CloneArray {
	
	public static void main(String[] args){
 
		int[] x = {788,2323,99,2323,2323,22222,3333};
		int[] y = x.clone();
		
		System.out.println(Arrays.toString(x));
		System.out.println(Arrays.toString(y)+"\n");
		
		x[1] = 66;
		
		System.out.println(Arrays.toString(x));
		System.out.println(Arrays.toString(y)+"\n");
		
		y[4] = 8888;
		
		System.out.println(Arrays.toString(x));
		System.out.println(Arrays.toString(y));
 
	}
}

Arrays.copyOf()

package com.java265.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);  
		
		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]

Arrays.copyOfRange()

package com.java265.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]

System.arraycopy()

package com.java265.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]
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: https://www.Java265.com/JavaJingYan/202301/16750917055587.html

最近发表

热门文章

好文推荐

Java265.com

https://www.java265.com

站长统计|粤ICP备14097017号-3

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

java爱好者