java中的foreach用法
2009-11-12 20:59:37 来源:WEB开发网 闂傚倸鍊搁崐鎼佸磹閹间礁纾归柟闂寸绾剧懓顪冪€n亜顒㈡い鎰Г閹便劌顫滈崱妤€骞婄紓鍌氬€瑰銊╁箟缁嬫鍚嬮柛顐線缂冩洟姊婚崒娆戭槮婵犫偓闁秵鎯為幖娣妼缁愭鏌″搴′簽濞戞挸绉甸妵鍕冀椤愵澀娌梺缁樻尪閸庣敻寮婚敐澶婂嵆闁绘劖绁撮崑鎾诲捶椤撴稑浜炬慨妯煎亾鐎氾拷

核心提示:view plaincopy to clipboardPRint?import java.util.*; public class ForeachExample { /** * @param args */public static void main(String[] args) { // TODO Auto-gen
view plaincopy to clipboardPRint?
import java.util.*;
public class ForeachExample {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Random r = new Random();
int[] x = new int[10];
for (int i = 0; i < x.length; i++) {
x[i] = r.nextInt(100);
}
// foreach的使用
for (int y : x) {
System.out.print(y + " ");
}
System.out.println();
// 另一种打印数组的方式
System.out.println(Arrays.toString(x));
for (char c : "I am a good girl!".toCharArray())
System.out.print(c + " ");
}
}
更多精彩
赞助商链接