【问题标题】:for cycle not works allright循环不能正常工作
【发布时间】:2010-03-22 16:06:50
【问题描述】:

我在这里发布的类中调用 addNotify() 方法。问题是,当我在代码中调用 addNotify() 时, setKeys(objs) 什么也不做。我的正在运行的应用程序的资源管理器中没有显示任何内容。

但是当我在没有循环的情况下调用 addNotify()(for int....),并且只向 ArrayList 添加一项时,它会正确显示一项。

有人知道哪里有问题吗?见割让

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);
}

}

【问题讨论】:

  • retag:这个q真的没有任何“netbeans-ness”。
  • 最初是为什么 addNotify 不起作用的问题。我在揭示问题本质后编辑了标题

标签: java for-loop


【解决方案1】:

看看这个循环:

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

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

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

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

【讨论】:

    【解决方案2】:

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

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

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

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

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

    【讨论】:

    • 这对我来说是个惊喜。虽然我没有太多的编程经验,但我用过很多次循环。直到现在,这从未发生在我身上。
    • 不,你还没有像这样使用for 循环并且让它工作,因为它不起作用
    • @Bombe - 我不写“像这样”。所以,是的,我用了几年的循环,我从来没有遇到过这个问题。
    • 当然,当正确使用for 循环时,它确实可以工作。不幸的是,“正确使用for 循环”与您所做的无关。 :)
    猜你喜欢
    • 2021-03-01
    • 2012-01-02
    • 2015-05-14
    • 2017-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多