对于使用二维数组每个循环 [英] For each loop using 2D array

查看:153
本文介绍了对于使用二维数组每个循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是Java的code的片段:

This is the snippet of Java code:

int[][] uu = new int[1][1];
uu[0][0] = 5;
for(int[] u: uu){
    System.out.println(u[0]);
}

它打印5.但是,为什么for循环声明的声明部分为int [] U ,而不是为int [] [ ] U

在UU您引用二维数组...
这不是功课。我对Java认证preparing。
干杯

At the uu you reference 2D array... That is not a homework. I am preparing for Java certification. Cheers

推荐答案

由于您的 UU 数组的数组。所以,当你迭代它,你会先得到一个阵列,然后你可以遍历数组来获得单个元素。

Since your uu is an array of array. So, when you iterate over it, you will first get an array, and then you can iterate over that array to get individual elements.

所以,你的外循环有 INT [] 类型,因此该声明。如果您通过 U 中多了一个内循环迭代,你会得到类型 INT : -

So, your outer loop has int[] as type, and hence that declaration. If you iterate through your u in one more inner loop, you will get the type int: -

for (int[] u: uu) {
    for (int elem: u) {
        // Your individual element
    }
}

这篇关于对于使用二维数组每个循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆