一、使用System类中的静态本地方法System.arraycopy(src, srcPos, dest, destPos, length)
src: 源数组
srcPos: 从源数组复制数据的起始位置
dest: 目标数组
destPos: 复制到目标数组的起始位置
length: 复制的长度
代码:
//复制数组
int a [] = new int[] {1,2,3,4,5};
// int b [] = new int[3];
// System.arraycopy(a,0,b,0,2);
// for(int each:b){
// System.out.println(each);
//
// }
二、使用Arrays.copyOfRange方法
第一个参数是要被复制的数组,第二个参数是从哪里开始复制,第三个参数是到哪里停止。 [from,to)左闭右开区间。
代码:
public class Main {
public static void main(String[] args) {
int a [] = new int[] { 18, 62, 68, 82, 65, 9 };
int b [] = Arrays.copyOfRange(a,0,a.length);
System.out.println(Arrays.toString(b));
}
}