java中的foreach用法
2009-11-12 20:59:37 来源:WEB开发网核心提示: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 + " ");
}
}
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 + " ");
}
}
更多精彩
赞助商链接