五种遍历二维数组的方式
import java.util.Arrays;
public class TwoArray {
public static void main(String[] args) {
int[][] arr1= new int[][]{{16, 66, 88, 2}, {2, 3, 5}};
one(arr1);
two(arr1);
three(arr1);
four(arr1);
five(arr1);
}
public static void one(int[][] arr){
for(int i=0;i<arr.length;i++){
for (int j=0;j<arr[i].length;j++){
System.out.print(arr[i][j]+"\t");
}
System.out.println();
}
System.out.println("-------------");
}
public static void two(int [][] arr){
for(int i=0;i<arr.length;i++){
for (int num:arr[i]) System.out.print(num+"\t");
System.out.println();
}System.out.println("-------------");
}
public static void three(int[][] arr){
for(int[] i:arr){
for (int j:i){
System.out.print(j+"\t");
}
System.out.println();
}System.out.println("-------------");
}
public static void four(int[][] arr){
for(int[] i:arr){
for(int j=0;j<i.length;j++){
System.out.print(i[j]+"\t");
}
System.out.println();
}System.out.println("-------------");
}
public static void five(int[][] arr){
for (int i=0;i<arr.length;i++){
System.out.println(Arrays.toString(arr[i]));
}System.out.println("-------------");
}
}
|