有什么区别
你能给我一些例子吗?
将对象添加到列表中时,我能够看到该对象正在替换列表中的所有值。
请检查下图,并注意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());
//}
}
}
Run Code Online (Sandbox Code Playgroud)