【发布时间】:2015-07-24 02:00:23
【问题描述】:
在将对象添加到列表时,我可以看到该对象正在替换列表中的所有值。
请检查下图并注意 for 循环中的代码是否存在列表中对象的重复项。
public static void main(String args[]) {
ArrayList<Modelclass> al = new ArrayList<Modelclass>();
Modelclass obj = new Modelclass();
for (int i = 0; i < 10; i++) {
obj.setName(2 + i);
obj.setRoolno(4 + i);
System.out.println(obj);
//if (!al.equals(obj)) {
al.add(obj);
System.out.println(obj.getName() + "" + obj.getRoolno());
//}
}
}
【问题讨论】:
-
邮政编码内联为文本。
标签: java arraylist collections core