因为周期不能正常工作

时间:2010-03-22 16:06:51

标签: java for-loop

我在我发布的类中调用addNotify()方法。问题是,当我在代码中调用addNotify()时,setKeys(objs)什么都不做。我的浏览器中没有显示正在运行的应用程序。

但是当我调用addNotify()而没有循环(对于int ....),并且只向ArrayList添加一个项目时,它会正确显示一个项目。

有谁知道问题出在哪里?见cede

class ProjectsNode extends Children.Keys{
private ArrayList objs = new ArrayList();

public ProjectsNode() {


}

    @Override
protected Node[] createNodes(Object o) {
    MainProject obj = (MainProject) o;
    AbstractNode result = new AbstractNode (new DiagramsNode(), Lookups.singleton(obj));
    result.setDisplayName (obj.getName());
    return new Node[] { result };
}

@Override
protected void addNotify() {
    //this loop causes nothing appears in my explorer.
    //but when I replace this loop by single line "objs.add(new MainProject("project1000"));", it shows that one item in explorer
    for (int i=0;i==10;i++){
        objs.add(new MainProject("project1000"));
    }
    setKeys (objs);
}

}

2 个答案:

答案 0 :(得分:5)

看看这个循环:

for (int i=0;i==10;i++)

这将从i = 0开始,并在i == 10 时继续。我想你的意思是:

for (int i = 0; i < 10; i++)

(为了清晰起见,添加了额外的空格。)

答案 1 :(得分:1)

Jon是对的......你的循环很可能是不正确的。

这是你的for循环到while循环的翻译,只是为了更清楚地阐明他的观察......

你的循环目前意味着......(在while-loop-ness中)

int i = 0;

while (i==10) {
    objs.add(new MainProject("project1000"));
    i++;
}
setKeys (objs);

永远不会调用addNotify,因为从不调用add ...