import java.util.Arrays; //for循环拷贝数组 public class KaoBei2 {//一维数组的拷贝 public static void main(String[] args) { // TODO 自动生成的方法存根 int[] array1={1,2,3,4,5,6,7}; int[] array2=new int[array1.length]; for(int i = 0;i < array1.length;i ){ array2[i] = array1[i]; } System.out.println(Arrays.toString(array2)); //二维数组的拷贝 int[][] array3= {{1,2,3},{4,5,6}}; int[][] array4 = new int[2][3]; for(int i = 0;i < array3.length;i ){ for(int j = 0;j < array3[i].length;j ){ array4[i][j] = array3[i][j]; } } System.out.println(Arrays.deepToString(array4)); } }
public static native void arraycopy(Object src, int srcPos,Object dest, int destPos,int length);
import java.util.Arrays; //System.arraycopy 相对较快的拷贝方式 native 方法 public class kaoBei1 { public static void main(String[] args) { // TODO 自动生成的方法存根 //一维数组的拷贝 int[] array = {1,2,3,4,5}; int[] array2 = new int[array.length]; System.arraycopy(array, 0, array2, 0, array.length);//(被复制的数组,从几号下标开始复制,复制到哪个数组,复制到新数组第几号下标,复制长度) System.out.println(Arrays.toString(array2)); //二维数组的拷贝 int[][] array1={{1,2,3,5,9},{2,3,36,5,7}}; int[][] array3=new int[2][5]; for(int i=0;i<array1.length;i ){ System.arraycopy(array1[i], 0, array3[i], 0, array1[i].length); } System.out.println(Arrays.deepToString(array3)); } }
public static byte[] copyOfRange(byte[] original, int from, int to) { int newLength = to - from; if (newLength < 0) throw new IllegalArgumentException(from " > " to); byte[] copy = new byte[newLength]; System.arraycopy(original, from, copy, 0, Math.min(original.length - from, newLength)); return copy; }
import java.util.Arrays; //Array.copyOf拷贝数组 public class KaoBei4 { public static void main(String[] args) { // TODO 自动生成的方法存根 //一维数组的拷贝 int[] array=new int[4]; System.out.println(Arrays.toString(array)); int[] array1 = new int[4]; array1 = Arrays.copyOf(array,array.length);//将数组array拷贝到数组brr,拷贝长度为array.length System.out.println(Arrays.toString(array1)); //一维数组的拷贝 int[][] array2={{1,2,3,4},{2,6,7,5}}; int[][] array3=new int[2][4]; array3=Arrays.copyOf(array2, array2.length); System.out.println(Arrays.deepToString(array3)); } }
import java.util.Arrays; //clone拷贝数组 public class KaoBei3 { public static void main(String[] args) { // TODO 自动生成的方法存根 //一维数组的拷贝 int[] array1={1,2,3,5,9,8,7}; int[] array2=new int[array1.length]; array2=array1.clone(); System.out.println(Arrays.toString(array2)); //二维数组的拷贝 int[][] array3={{1,2,3,4},{5,6,7}}; int[][] array4=new int[2][]; for(int i = 0;i < array3.length;i ){ array4[i] = array3[i].clone(); } for(int i=0;i<array3.length;i ){ System.out.print(Arrays.toString(array4[i])); } } }
protected native Object clone() throws CloneNotSupportedException;
public Object clone() throws CloneNotSupportedException { // TODO Auto-generated method stub return super.clone(); }
class Aby implements Cloneable{ public int i; public Aby(int i) { this.i = i; } @Override public Object clone() throws CloneNotSupportedException { // TODO Auto-generated method stub return super.clone(); } } public class Solution { public static void main(String[] args) throws CloneNotSupportedException { Aby aby1 = new Aby(1); Aby aby2 = (Aby) aby1.clone(); aby1.i = 2; System.out.println(aby1.i); //2 System.out.println(aby2.i); //1 Aby[] arr = {aby1,aby2}; Aby[] arr2 = arr.clone(); arr2[0].i = 3; System.out.println(arr[0].i); //3 System.out.println(arr2[0].i); //3 } }