1.使用for循環進行複制
2.使用函數System.Arraycopy()進行複制
3.使用數組名.copy()進行複制
代碼如下:
public class fuzhi{
public static void main(String[] args) {
//使用for循環指派數組
//定義一個double類型數組
double[] x=new double[] {1,2,3,4,5,6,7};
//在定義一個指派數組
double[] y =new double[x.length];
for(int i=0;i<x.length;i++) {
y[i]=x[i];
}
System.out.println("指派後y數組的值為:"+Arrays.toString(y));
//使用System.arraycopy()方法複制數組x
//定義一個數組
double[] z=new double[] {10,20,30,40,50,60,70};
System.arraycopy(x, 1, z, 2, 5);
System.out.println("複制後的z數組元素為:"+Arrays.toString(z));
//使用clone方法複制數組x
//定義一個數組k
double[] k;
k=x.clone();
System.out.println("複制後k的數組元素為:"+Arrays.toString(k));
}
}