class Data { int val; int getVal() { returnval; } void setVal(int val) { this.val= val; } } public class ListTest { public static void main(String argv[]) { Data data = new Data(); ArrayList list = new ArrayList(); for (int i=100; i<103; i++) { data.setVal(i); list.add(data); } int j = 0; while (j < list.size()) { Data tmp = (Data )list.get(j); System.out.println("list(" + j + ") = " +tmp.getVal()); j++; } } }
看了看结果没有明白?怎么会输出都一样呢?list(0)=102
|