java中arraycopy的用法_[jdk源码阅读系列]Java中System.arraycopy()的用法

本文转载,原文链接:

3分钟了解Java中System.arraycopy的用法 - 伊万夫斯基 - 博客园  https://www.cnblogs.com/benjieqiang/p/11428832.html

3分钟了解Java中System.arraycopy的用法

System提供了一个静态方法arraycopy(),我们可以使用它来实现数组之间的复制。其函数原型是:

public static native void arraycopy(Object src,int srcPos,Object dest, int destPos,int length);

*@param src the source array. 源数组*@param srcPos starting position in the source array. 源数组的起始位置*@param dest the destination array. 目标数组*@param destPos starting position in the destination data. 目标数组的起始位置* @param length the number of array elements to be copied. 复制的长度

举个栗子:

将array数组复制到新的数组中;

int[] array = {1, 2, 3, 4, 5};int[] targetArr = new int[array.length];

System.arraycopy(array,0,targetArr,0,array.length);

本文转载,原文链接:

Java-Java中System.arraycopy() 和 Arrays.copyOf()两者之间的区别 - 夜行过客 - 博客园  https://www.cnblogs.com/yongdaimi/p/5995414.html

Java-Java中System.arraycopy() 和 Arrays.copyOf()两者之间的区别

如果我们想拷贝一个数组,我们可能会使用System.arraycopy()或者Arrays.copyof()两种方式。在这里,我们将使用一个比较简单的示例来阐述两者之间的区别。

1、示例代码:

System.arraycopy()

int[] arr = {1,2,3,4,5};

int[] copied = new int[10];

System.arraycopy(arr, 0, copied, 1, 5);//5 is the length to copy

System.out.println(Arrays.toString(copied));

运行结果:

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

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

Arrays.copyof()

int[] copied = Arrays.copyOf(arr, 10); //10 the the length of the new array

System.out.println(Arrays.toString(copied));

copied = Arrays.copyOf(arr, 3);

System.out.println(Arrays.toString(copied));

运行结果:

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

[1, 2, 3]

2、两者间的主要区别

两者的区别在于,Arrays.copyOf()不仅仅只是拷贝数组中的元素,在拷贝元素时,会创建一个新的数组对象。而System.arrayCopy只拷贝已经存在数组元素。

如果我们看过Arrays.copyOf()的源码就会知道,该方法的底层还是调用了System.arrayCopyOf()方法。

public static int[] copyOf(int[] original, int newLength) {

int[] copy = new int[newLength];

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

return copy;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!以下是JavaSystem类arraycopy方法的简化版源码: ```java public class System { public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length) { // 源数组和目标数组必须是对象数组 if (!(src instanceof Object[] && dest instanceof Object[])) { throw new ArrayStoreException("Source and destination arrays must be of the same type"); } // 检查数组范围 if (srcPos < 0 || destPos < 0 || length < 0 || srcPos + length > ((Object[]) src).length || destPos + length > ((Object[]) dest).length) { throw new IndexOutOfBoundsException(); } // 执行数组复制 if (src != dest) { if (srcPos < destPos) { // 从后往前复制 for (int i = length - 1; i >= 0; i--) { ((Object[]) dest)[destPos + i] = ((Object[]) src)[srcPos + i]; } } else { // 从前往后复制 for (int i = 0; i < length; i++) { ((Object[]) dest)[destPos + i] = ((Object[]) src)[srcPos + i]; } } } } } ``` 上述源码展示了System类的arraycopy方法的实现。该方法用于将源数组的元素复制到目标数组。在执行复制前,该方法会进行数组类型的检查和范围的检查。如果源数组和目标数组不是对象数组,将抛出ArrayStoreException异常。如果索引位置或长度超出数组的范围,将抛出IndexOutOfBoundsException异常。最后,根据源数组和目标数组的位置关系,使用循环逐个复制元素到目标数组。 请注意,这只是arraycopy方法的简化版源码,实际源码可能会更复杂并涉及一些优化。此外,该源码可能与特定的Java版本有所不同,以上代码仅供参考。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值