2023-05-12 Java中数组的几种拷贝方法,clone() 、 System.arraycopy,Arrays.copyOf,CopyOfRange(),实例运行测试

一、for循环拷贝是最简单直观的方法,也是最low的方法,这里不做介绍。

二、使用 clone() 方法,使用clone方法进行拷贝时,是将arr中的所有元素都拷贝下来,不可以挑选。 clone() 方法的返回值是 Object 类型,要使用强制类型转换为适当的类型。

byte[] targetArray=(byte[])sourceArray.clone();
import java.util.Arrays;

public class Main {
	public static void main(String[] args) {
	    byte []data = {0,1,2,3,20,100,(byte)200,(byte)250};
	    byte []image = data.clone();
		System.out.println("data :"+Arrays.toString(data));
		System.out.println("image:"+Arrays.toString(image));
	}
}
data :[0, 1, 2, 3, 20, 100, -56, -6]
image:[0, 1, 2, 3, 20, 100, -56, -6]

三、Arrays类中的 System.arraycopy方法

 System.arraycopy(original, 0, copy, 0,
                         Math.min(original.length, newLength));

import java.util.Arrays;

public class Main {
	public static void main(String[] args) {
	    byte []data = {0,1,2,3,20,100,(byte)200,(byte)250};
	    byte []image = new byte[12];
	    System.arraycopy(data,2,image,1,3);
		System.out.println("data :"+Arrays.toString(data));
		System.out.println("image:"+Arrays.toString(image));
	}
}
标准输出:
data :[0, 1, 2, 3, 20, 100, -56, -6]
image:[0, 2, 3, 20, 0, 0, 0, 0, 0, 0, 0, 0]

四、使用Arrays类中 CopyOfRange() 方法对数组进行复制该方法是Arrays 类中的 CopyOfRange() 方法,该方法用于拷贝某个范围.看其中的代码可以知道底层还是调用System.arraycopy方法。

Arrays.copyOfRange(dataType[] srcArray,int startIndex,int endIndex)
    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;
    }

import java.util.Arrays;

public class Main {
	public static void main(String[] args) {
	    byte []data  = {0,1,2,3,20,100,(byte)200,(byte)250};
	    byte []image = Arrays.copyOfRange(data,2,5);
		System.out.println("data :"+Arrays.toString(data));
		System.out.println("image:"+Arrays.toString(image));
	}
}
标准输出:
data :[0, 1, 2, 3, 20, 100, -56, -6]
image:[2, 3, 20]

nt[] original:将要拷贝的原数组
int from:拷贝数组的起始下标 ,下标值必须在 0 到原数组长度之间;

int to:拷贝数组的终止下标,且终止下标必须大于等于起始下标,也可以大于 arr.length,如果大于 arr.length,则新数组中剩余的空位置用默认值0进行填充。

上面复制的是【2到5),不包括5项。

五、Arrays类中的Arrays.copyOf方法,实现的方法也是调用System.arraycopy。


import java.util.Arrays;

public class Main {
	public static void main(String[] args) {
	    byte []data  = {0,1,2,3,20,100,(byte)200,(byte)250};
	    byte []image = Arrays.copyOf(data,data.length-1);
		System.out.println("data :"+Arrays.toString(data));
		System.out.println("image:"+Arrays.toString(image));
	}
}
标准输出:
data :[0, 1, 2, 3, 20, 100, -56, -6]
image:[0, 1, 2, 3, 20, 100, -56]

六、参考文章

Java中数组的5种拷贝方法_心皿月的博客-CSDN博客

Java数组复制(拷贝)的4种方法_java 数组复制_命殿的博客-CSDN博客

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值