Item 57. Local Variable Scope
์ง์ญ๋ณ์ ๋ฒ์๋ฅผ ์ต์ํํ๋ ๋ฐฉ๋ฒ
class Example {
public static void main(String[] args) {
// for-each ๋ฌธ
for (Element e : c) {
doSomething(e);
}
// ๋ฐ๋ณต์๊ฐ ํ์ํ ๊ฒฝ์ฐ for๋ฌธ ์ฌ์ฉ
for (Iterator<Element> i = c.iterator(); i.hasNext(); ) {
Element e = i.next();
doSomething(e, i);
}
// while๋ฌธ ์ฌ์ฉ ์..
Iterator<Element> i = c.iterator();
while (i.hasNext()) {
doSomething(i.next());
}
Iterator<Element> i2 = c.iterator();
while (i.hasNext()) { // ์ด๋ฏธ ์ํํ i๋ฅผ ๋ค์ ์ํ ์๋
doSomething(i2.next()); // ์คํ๋์ง ์์
}
}
}Last updated