java使用foreach语句遍历二维数组如何实现?

弱弱的问一句:阶梯数组可以用foreach吗??

第1个回答  2010-09-26
public class Tautog { //创建类
public static void main(String[] args) { //主方法
int arr2[][] = { { 4, 3 }, { 1, 2 } }; //定义二维数组
System.out.println("数组中的元素是:"); //提示信息
for (int x[] : arr2) { //外层循环变量为一维数组
for (int e : x) { //循环遍历每一个数组元素
if (e == x.length) { //判断变量是二维数组中的最后一个元素
System.out.print(e); //输出二维数组的最后一个元素
} else //如何不是二维数组中的最后一个元素
System.out.print(e + "、"); //输出信息
}
}
}
}
JDK1.5以上才能
第2个回答  2010-09-26
Java里面是有for循环的吧。抱着学习的观念来回答你的问题。参考了一下楼上两位的,自己写了个程序:
public class TestArray {

public static void main(String[] args) {
// 定义一个长度为10的数组
int a[][] = new int[10][8];
// 为数组中每个元素赋值
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++)
a[i][j] = i * j;

}

//用泛型遍历输出数组
for(int[] arr : a){
for(int arr2 : arr){
System.out.print(arr2+"\t");
}
//每打印完一个a[i][0]-a[i][8]就换行
System.out.println();
}
}

}本回答被提问者采纳
第3个回答  2012-03-09
自己试试。 习惯用for
第4个回答  2010-09-26
<c:forEach items="${array}" var="item">
<c:forEach items="${item}" var="data">
<span>${data}</span>
</c:forEach>
</c:forEach>
第5个回答  2010-09-26
public class Test2 {
public static void main(String[] args) throws Exception {

int[][] arr = new int[][]{new int[]{2,3},new int[]{4,5}};
for (int[] is : arr) {
for (int i : is) {
System.out.print(i + ", ");
}
System.out.println();
}
}
}