对于一维数组,就是用一个for循环让它们实现遍历;
public class Fori {
public static void main(String[] args) {
int[] arr=new int[]{1,2,3,3,2,1};
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i]+" ");
}
}
}
运行结果截图:
对于二维数组,跟一维数组的遍历其实是一样的,就是多了个for循环语句而已;
for (int j = 0; j < b[i].length; j++){}
全部代码如下:
public class For {
public static void main(String[] args) {
int b[][]=new int[][]{{1},{2,3},{4,5,6}};
System.out.println("该二维数组是:");
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b[i].length; j++) {
System.out.print(b[i][j]+" ");
}
System.out.println(); //换行
}
}
}
运行结果截图: