【发布时间】:2013-10-05 14:45:43
【问题描述】:
例如:
public class Box{
public int id;
public Box(int id){
this.id = id;
}
}
ArrayList<Box> boxArray = new ArrayList<Box>();
boxArray.add(new Box(0));
boxArray.add(new Box(1));
boxArray.add(new Box(2));
for (Iterator<Box> iter = boxArray.iterator(); iter.hasNext();) {
Box box = iter.next();
if (box.id == 1) {
iter.remove();
box = null;
}
}
我知道:在 Java 中,JM 在没有引用时是删除对象。
但是
- 我是否应该设置“box = null;”是否从数组中删除此代码后在此代码中添加对象?
- 如何确保该对象肯定会从 JM 中删除?
【问题讨论】:
-
你不知道,除非你知道它在其他地方被引用。