通过System这个类的arraycopy方法将已知数组int [] arr ={12,234,45,324};
中中间两个元素拷贝到另外一个新数组中
package demo5;
/*
* 通过System这个类的arraycopy方法将已知数组int [] arr ={12,234,45,324};
* 中间两个元素拷贝到另外一个新数组中;
*/
public class SystemTest {
public static void main(String[] args) {
int[] arr = {12,234,45,324};
/*
* static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
* 复制数组
* 参数1:源数组
* 参数2:源数组的起始索引位置
* 参数3:目标数组
* 参数4:目标数组的起始索引位置
* 参数5:指定接受的元素个数
*/
int[] arrCopy = new int[arr.length];
System.arraycopy(arr, 1, arrCopy, 0, 2);
for(int i=0; i<arrCopy.length; i++) {
System.out.print(arrCopy[i]+" ");//234 45 0 0
}
System.out.println();
int[] arrCopy1 = new int[arr.length];
System.arraycopy(arr, 1, arrCopy1, 1, 2);
for(int i=0; i<arrCopy1.length; i++) {
System.out.print(arrCopy1[i]+" ");//0 234 45 0
}
}
}

本文介绍如何使用Java中的System.arraycopy方法从已知数组int[]arr={12,234,45,324}
1222

被折叠的 条评论
为什么被折叠?



