public class ArrayCopy { public static void main(String[] args) { String[] s = {"M", "I", "S", "O", "A"}; String[] b = new String[6]; System.arraycopy(s, 0, b, 0, s.length); b[2] = "asdasd"; System.out.println(s[2]);//s System.out.println(b[2]);//asdasd int[][] a = {{1, 2, 3}, {2, 4, 5}, {1, 3, 4}}; int[][] a2 = new int[3][]; System.arraycopy(a, 0, a2, 0, a.length); a2[0][0] = 12; System.out.println(a[0][0]);//12 }}二维数组拷贝后拷贝后的数组改变拷贝前的数组也改变,但为什么String类型的数组不会有同样的现象呢。不都是引用对象吗。
2 回答
撒科打诨
TA贡献1934条经验 获得超2个赞
你好好运行一下看看,第二个输出是:1,不是12。。。。
public class Test {
public static void main(String[] args) {
String[] s = { "M", "I", "S", "O", "A" };
String[] b = new String[6];
System.arraycopy(s, 0, b, 0, s.length);
b[2] = "asdasd";
System.out.println(s[2]);// s
System.out.println(b[2]);// asdasd
int[][] a = { { 1, 2, 3 }, { 2, 4, 5 }, { 1, 3, 4 } };
int[][] a2 = new int[3][3];
System.arraycopy(a[0], 0, a2[0], 0, a[0].length);
a2[0][0] = 12;
System.out.println(a[0][0]);// 1
}
}
添加回答
举报
0/150
提交
取消