用for-each循环迭代多维数组

原创
2015/12/07 18:26
阅读数 142
package pratise;

public class Construct {

    public static void main(String[] args) {
        int sum = 0;
        int nums[][] = new int[3][5];
        
        //give nums some values
        for(int i = 0; i < 3; i++){
            for(int j = 0; j < 5; j++){
                nums[i][j] = (i+1)*(j+1);
            }
        }
        
        //Use for-each for loop to display and sum the values.
        for(int x[]: nums){
            for(int y: x){
                System.out.print(y + " ");
                sum += y;
            }
        }
        System.out.println("Summation: " + sum);
    }

}


展开阅读全文
加载中
点击加入讨论🔥(1) 发布并加入讨论🔥
打赏
1 评论
0 收藏
0
分享
返回顶部
顶部