简体   繁体   English

打印3D数组中的所有值

[英]Print all values in a 3d array

Is there a way to print all values in a 3d array? 有没有一种方法可以打印3d数组中的所有值?

This is what I got but getting a null pointer exception: 这是我得到的,但是得到了空指针异常:

        int i = 2;
        int x = 15;
        String[][][] arrays = new String[x][x][i];
        String arraytext = "hello";


            for (String[][] row: arrays)
             Arrays.fill(row, arraytext);


            for (int a = 0; a<=x; a++){
              for (int b = 0; b<=x; b++){
                  for (int j = 0; j<=i; j++)
                      {System.out.println(arrays[a][b][j]);}

                                    }
                                }

You have a problem filling your array. 您在填充数组时遇到问题。 you are getting 你越来越

java.lang.ArrayStoreException: java.lang.String at Arrays.fill(row, arraytext);

That is cause you are trying to add a String when is expected an String[][]. 这是因为您试图在需要String [] []时添加String。 And for printing you can use Arrays.deepToString() is your answer. 对于打印,您可以使用Arrays.deepToString()是您的答案。

System.out.println(Arrays.deepToString(arrays));

Example: 例:

public static void main(String[] args) {
        String[][][] array3d = new String[10][10][10];

         for(String [] [] array2d : array3d){
             for(String[] array : array2d){
                 Arrays.fill(array, "hello");
             }
         }

         System.out.println(Arrays.deepToString(array3d));

    }

The problem is that this: 问题是:

for (String[][] row: arrays)
     Arrays.fill(row, arraytext);

Is trying to pass row , which is a double array of strings, to a function expecting a single array of strings. 正在尝试将row (它是字符串的精度数组)传递给期望使用单个字符串数组的函数。 To get the bottom level of your 3D array, you want this: 要获得3D阵列的最底层信息,您需要这样做:

for (String[][] slice: arrays)
    for (String[] row: slice)
        Arrays.fill(row, arraytext);

You also have both issues addressed by other answers in your double loop: You'll get an index out of bounds unless you change the <= s to < , and sub isn't defined. 双循环中的其他答案也解决了这两个问题:除非将<=更改为< ,并且未定义sub ,否则您将获得一个超出范围的索引。

You're going out of bounds on your arrays, instead of <= try < . 您将超出数组的范围,而不是<= try <

for (int a = 0; a < x; a++){
          for (int b = 0; b < x; sub++){
              for (int j = 0; j < i; j++)
                  {System.out.println(arrays[a][b][j]);}

                                }
                            }

Also, what is sub ? 另外,什么是sub You're code snippet should show where you define this variable. 您的代码段应显示在何处定义此变量。

Looks like you are experiencing off by 1 error (use < instead of <= ): 看起来您遇到了1个错误(使用<代替<= ):

for (int a = 0; a<x; a++){
   for (int b = 0; b<x; b++){
      for (int j = 0; j<i; j++)
      {System.out.println(arrays[a][b][j]);}

   }
}

Since you declared arrays as 由于您将arrays声明为

String[][][] arrays = new String[x][x][i];

the bounds are from 0 to x-1 for the first two indexes, and 0 to i-1 for the thrid index. 前两个索引的范围从0到x-1 ,第三索引的范围从0到i-1

另外,在第二个循环中,您要递增未知变量sub而不是b

You just forgot to replace a temporary value from copy-pastaing 您只是忘了替换复制粘贴中的临时值

for (int a = 0; a<=x; a++){
    for (int b = 0; b<=x; sub++){ // sub here <---
        for (int j = 0; j<=i; j++) {
            System.out.println(arrays[a][b][j]);
        }
    }
}

Also see @jh314 's answer, your doing the off by one error 另请参阅@ jh314的答案,您因一个错误而关闭

package academy.learnprogramming; 打包academy.learnprogramming;

import java.util.Arrays; 导入java.util.Arrays;

public class Print3DArray { 公共类Print3DArray {

public static void main(String[] args) {

    // declare & initialize an int 3D array firstInt3D
    int[][][] firstInt3D = {
            {{1,2}},
            {{3,4}},
            {{5,6,7,8},{9,10}},
            {{11},{12,13,14}},
    };



    // print all values of firstInt3D page by page
    int x = 0;
    for (int[][] accessPage: firstInt3D){
        System.out.println("page:" + x);
        if (x < firstInt3D.length) x++;
        for (int[] accessRow: accessPage){
            System.out.println(Arrays.toString(accessRow));
        }
        System.out.println();
    }
}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM