美文网首页
第 57 条:将局部变量的作用域最小化

第 57 条:将局部变量的作用域最小化

作者: 综合楼 | 来源:发表于2021-06-01 23:51 被阅读0次
    将局部变量的作用域最小化.jpeg
    Iterator<Element> i = c.iterator();
    while (i.hasNext()) {
        doSomething(i.next());
    }
    ...
    Iterator<Element> i2 = c2.iterator();
    while (i.hasNext()) { // BUG!
        doSomethingElse(i2.next());
    }
    
    for (Iterator<Element> i = c.iterator(); i.hasNext(); ) {
        Element e = i.next();
        ... // Do something with e and i
    }
    ...
    // Compile-time error - cannot find symbol i
    for (Iterator<Element> i2 = c2.iterator(); i.hasNext(); ) {
        Element e2 = i2.next();
        ... // Do something with e2 and i2
    }
    

    相关文章

      网友评论

          本文标题:第 57 条:将局部变量的作用域最小化

          本文链接:https://www.haomeiwen.com/subject/rfrsjltx.html