Created
July 9, 2010 11:35
-
-
Save kmizu/469371 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Foreach { | |
public static void main(String[] args) { | |
int j = 0; | |
Runnable[] rs = new Runnable[3]; | |
for(final int i:new int[]{3, 5, 7}) { | |
rs[j] = new Runnable() { | |
public void run() { | |
//別々のiをキャプチャーしているか? | |
System.out.println(i); | |
} | |
}; | |
j++; | |
} | |
/* | |
* 3 | |
* 5 | |
* 7 | |
* と表示される | |
*/ | |
for(Runnable r:rs) r.run(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment