java 对多维数组的工具类(比如遍历多维数组工具类)

目录

1 遍历int类型二维数组

public static void printErIntArray(int[][] matrix){

        for (int i = 0; i < matrix.length; i++) { //this equals to the row in our matrix.
            for (int j = 0; j < matrix[i].length; j++) { //this equals to the column in each row.
                System.out.println(matrix[i][j] + " ");
            }
//            System.out.println("/t"); //change line on console as row comes to end in the matrix.
        }
    }

复制

2 使用流遍历int 类型二维数组

 public static void printErIntArrayByStream(int[][] iDim) {
        Arrays.stream(iDim).forEach(i -> {
            Arrays.stream(i).forEach(n -> System.out.println(n));
            System.out.println();
        });

    }

复制

3 遍历 float 类型二维数组

public static void printErFloatArray(float[][] matrix){

    for (int i = 0; i < matrix.length; i++) { //this equals to the row in our matrix.
        for (int j = 0; j < matrix[i].length; j++) { //this equals to the column in each row.
            System.out.println(matrix[i][j] + " ");
        }
//            System.out.println("/t"); //change line on console as row comes to end in the matrix.
    }
}

复制

4 遍历 double 类型二维数组

public static void printErDoubleArrayByStream(double[][] matrix){
    Arrays.stream(matrix).forEach(i -> {
        Arrays.stream(i).forEach(n -> System.out.format(Locale.US,"%.2f ", n));
        System.out.println();
    });
}

复制

5 遍历 int 类型 3维数据

  public static void printThreeIntArray(int[][][] arr){
        for(int i=0;i<arr.length;i++) {
            for(int j=0;j<arr[i].length;j++) {        //我们可以把前边想像成一个一维数组
                if(j==arr[i].length-1) {
                    System.out.println();            //换行
                }
                for(int k=0;k<arr[i][j].length;k++)
                    System.out.print(arr[i][j][k]+" ");//遍历三维数组
            }
        }


    }

复制

6 遍历 Float 类型 3维数据

    public static void printThreeFloatArray(float[][][] arr){
        for(int i=0;i<arr.length;i++) {
            for(int j=0;j<arr[i].length;j++) {        //我们可以把前边想像成一个一维数组
                if(j==arr[i].length-1) {
                    System.out.println();            //换行
                }
                for(int k=0;k<arr[i][j].length;k++)
                    System.out.print(arr[i][j][k]+" ");//遍历三维数组
            }
        }
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值