首页 > 学院 > 开发设计 > 正文

java中的foreach用法

2019-11-17 04:04:29
字体:
来源:转载
供稿:网友
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 + " ");   
    }   
}   
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表