public static void main(String[] args) {
int[][] array1 = {{10,10,10},{10,10,10}};
int[][] array2 = new int[2][3];
for(int i = 0;i < array1.length;i++){
for(int j = 0;j < array1[0].length;j++){
array2[i][j] = array1[i][j];
}
}
System.out.println("数组1 ");
show(array1);
System.out.println("数组2 ");
show(array2);
array2[0][0] = 30;
System.out.println("===修改后===");
System.out.println("数组1 ");
show(array1);
System.out.println("数组2 ");
show(array2);
}
运行结果:
数组1
[10,10,10,]
[10,10,10,]
数组2
[10,10,10,]
[10,10,10,]
=修改后=
数组1
[10,10,10,]
[10,10,10,]
数组2
[30,10,10,]
[10,10,10,]
public static void main(String[] args) {
test[][] array1 = {{new test(),new test(),new test()},
{new test(),new test(),new test()}};
test[][] array2 = new test[2][3];
for(int i = 0;i < array1.length;i++){
for(int j = 0;j < array1[0].length;j++){
array2[i][j] = array1[i][j];
}
}
System.out.println("数组1 ");
show(array1);
System.out.println("数组2 ");
show(array2);
array2[0][0].setVal(30);
System.out.println("===修改后===");
System.out.println("数组1 ");
show(array1);
System.out.println("数组2 ");
show(array2);
}
运行结果:
数组1
[10,10,10,]
[10,10,10,]
数组2
[10,10,10,]
[10,10,10,]
=修改后=
数组1
[30,10,10,]
[10,10,10,]
数组2
[30,10,10,]
[10,10,10,]
public static void main(String[] args) {
int[][] array1 = {{10,10,10},{10,10,10}};
int[][] array2 = new int[2][3];
for(int i = 0;i < array1.length;i++){
array2[i] = array1[i].clone();
}
System.out.println("数组1 ");
show(array1);
System.out.println("数组2 ");
show(array2);
array2[0][0] = 30;
System.out.println("===修改后===");
System.out.println("数组1 ");
show(array1);
System.out.println("数组2 ");
show(array2);
}
运行结果:
数组1
[10,10,10,]
[10,10,10,]
数组2
[10,10,10,]
[10,10,10,]
=修改后=
数组1
[10,10,10,]
[10,10,10,]
数组2
[30,10,10,]
[10,10,10,]
public static void main(String[] args) {
test[][] array1 = {{new test(),new test(),new test()},
{new test(),new test(),new test()}};
test[][] array2 = new test[2][3];
for(int i = 0;i < array1.length;i++){
array2[i] = array1[i].clone();
}
System.out.println("数组1 ");
show(array1);
System.out.println("数组2 ");
show(array2);
array2[0][0].setVal(30);
System.out.println("===修改后===");
System.out.println("数组1 ");
show(array1);
System.out.println("数组2 ");
show(array2);
}
运行结果:
数组1
[10,10,10,]
[10,10,10,]
数组2
[10,10,10,]
[10,10,10,]
=修改后=
数组1
[30,10,10,]
[10,10,10,]
数组2
[30,10,10,]
[10,10,10,]
public static void main(String[] args) {
int[][] array1 = {{10,10,10},{10,10,10}};
int[][] array2 = new int[2][3];
for(int i = 0;i < array1.length;i++){
System.arraycopy(array1[i],0,array2[i],0,array1[i].length);
}
System.out.println("数组1 ");
show(array1);
System.out.println("数组2 ");
show(array2);
array2[0][0] = 30;
System.out.println("===修改后===");
System.out.println("数组1 ");
show(array1);
System.out.println("数组2 ");
show(array2);
}
}
运行结果:
数组1
[10,10,10,]
[10,10,10,]
数组2
[30,10,10,]
[10,10,10,]
public static void main(String[] args) {
test[][] array1 = {{new test(),new test(),new test()},
{new test(),new test(),new test()}};
test[][] array2 = new test[2][3];
for(int i = 0;i < array1.length;i++){
System.arraycopy(array1[i],0,array2[i],0,array1[i].length);
}
System.out.println("数组1 ");
show(array1);
System.out.println("数组2 ");
show(array2);
array2[0][0].setVal(30);
System.out.println("===修改后===");
System.out.println("数组1 ");
show(array1);
System.out.println("数组2 ");
show(array2);
}
运行结果:
数组1
[10,10,10,]
[10,10,10,]
数组2
[10,10,10,]
[10,10,10,]
=修改后=
数组1
[30,10,10,]
[10,10,10,]
数组2
[30,10,10,]
[10,10,10,]
public static void main(String[] args) {
int[][] array1 = {{10,10,10},{10,10,10}};
int[][] array2 = new int[2][3];
for(int i = 0;i < array1.length;i++){
array2[i] = Arrays.copyOf(array1[i],array1[i].length);
}
System.out.println("数组1 ");
show(array1);
System.out.println("数组2 ");
show(array2);
array2[0][0] = 30;
System.out.println("===修改后===");
System.out.println("数组1 ");
show(array1);
System.out.println("数组2 ");
show(array2);
}
运行结果:
数组1
[10,10,10,]
[10,10,10,]
数组2
[10,10,10,]
[10,10,10,]
=修改后=
数组1
[10,10,10,]
[10,10,10,]
数组2
[30,10,10,]
[10,10,10,]
public static void main1(String[] args) {
test[][] array1 = {{new test(),new test(),new test()},
{new test(),new test(),new test()}};
test[][] array2 = new test[2][3];
for(int i = 0;i < array1.length;i++){
array2[i] = Arrays.copyOf(array1[i],array1[i].length);
}
System.out.println("数组1 ");
show(array1);
System.out.println("数组2 ");
show(array2);
array2[0][0].setVal(30);
System.out.println("===修改后===");
System.out.println("数组1 ");
show(array1);
System.out.println("数组2 ");
show(array2);
}
运行结果:
数组1
[10,10,10,]
[10,10,10,]
数组2
[10,10,10,]
[10,10,10,]
=修改后=
数组1
[30,10,10,]
[10,10,10,]
数组2
[30,10,10,]
[10,10,10,]
从上面几个例子我们可以看出,基本类型的拷贝都是深拷贝,引用类型的拷贝都是浅拷贝,这点和一维数组的拷贝没差。
一维数组的拷贝参考博文:java 基础——一维数组拷贝
关于深拷贝和浅拷贝也请参考上篇博文。