ArrayList<ArrayList<ArrayList<ArrayList<String>>>> myEmpls = new ArrayList<>();
ArrayList<ArrayList<ArrayList<String>>> dummy2 = new ArrayList<>();
ArrayList<ArrayList<String>> dummy = new ArrayList<>();
for (int x = 0; x < 4; x++) {
myEmpls.add(dummy2);
}
System.out.println(myEmpls.size());
// returns 4 as expected
System.out.println(myEmpls.get(0).size());
System.out.println(myEmpls.get(1).size());
// both return 0 as expected
for (int x = 0; x < 10; x++) {
myEmpls.get(0).add(dummy);
}
System.out.println(myEmpls.get(1).size());
// returns 10 altough I added to myEmpls.get(0) and not
// myEmpls.get(1) so I expected this to still be 0.
有人可以解释为什么是这样的情况?也许我错过了一些明显的东西。
在此先感谢。
都不尽dummy2s一样吗? – ChiefTwoPencils
感谢所有的答案,我之前遇到过同样的问题,但由于我没有编码一段时间,所以我没有认出它! – user2299050