以下是归纳的JAVA中复制数组的方法:
1. 使用FOR循环,将数组的每个元素复制或者复制指定元素,不过效率差一点
2. 使用clone方法,得到数组的值,而不是引用,不能复制指定元素,灵活性差一点
3. 使用System.arraycopy(src, srcPos, dest, destPos, length)方法,推荐使用
举例:
1.使用FOR循环
就不说啦
int[] src={1,3,5,6,7,8};
int[] dest;
dest=(int[]) src.clone();//使用clone创建
副本,注意clone要使用强制转换
3.使用System.arraycopy
int[] src={1,3,5,6,7,8};
int[] dest = new int[6];
System.arraycopy(src, 0, dest, 0, 6);