首页 > 编程知识 正文

java binarysearch,DataInputStream

时间:2023-05-04 11:06:29 阅读:171565 作者:4166

正文转载,原文链接:

三分钟了解如何在Java中使用System.arraycopy伊万夫斯基-博客圈https://www.cn blogs.com/Benji eqiang/p/11428832.html

3分钟了解如何使用Java的System.arraycopy

System提供了静态方法arraycopy (),您可以使用该方法在数组之间进行复制。 函数原型如下

publicstaticnativevoidarraycopy (object src,int srcPos,Object dest,int destPos,int length );

*@param src the source array .源数组* @ paramsrcposstartingpositioninthesourcearray .源数组的起始位置* @param dest the destination array .目标数组@ paramdestposstartingpositioninthedestinationdata .目标数组的开始位置* @ paramlengththenumenumation

提心吊胆:

将数组复制到新数组;

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.cn blogs.com/yongdaimi/p/5995414

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 ]

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 )方法。

公共静态int [ ] copy of (int [ ] original,int newLength ) {

int[] copy=new int[newLength];

system.arraycopy(original,0,copy,0,math.min ) original.length,newLength );

返回副本;

}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。