【问题标题】:Output of 2-dimensional arrays in java , (take this error [[Z@6d06d69c )java中二维数组的输出,(取这个错误[[Z@6d06d69c)
【发布时间】:2021-07-16 21:06:59
【问题描述】:
public class Main {
    boolean[][] Obstacle;
    int rows; 
    int cols; 

    Main(int rows, int cols) {
        this.rows = rows;
        this.cols = cols;
        this.Obstacle = new boolean[rows][cols];
        Board();
    }

    public void Board() {
        int i = 0, j = 0;
        Random r = new Random();
        for (i = 0; i < 5;) {
            int x = r.nextInt(this.cols);
            int y = r.nextInt(this.rows);
            if (Obstacle[x][y] == true) {
                Obstacle[x][y] = false;
                i++;
            }
        }
    }

    public static void main(String[] args) {
        Main m = new Main(6, 8, 10);
        System.out.print(m.Obstacle);

    }
}

您好,首先我阅读了有关该问题的所有问题,但找不到答案,我仍然遇到同样的错误。通常我知道二维布尔数组默认为假,但我无法得到这样的输出。什么问题,你能帮忙吗?

我得到这个输出:

[[Z@6d06d69c

【问题讨论】:

    标签: java arrays class boolean


    【解决方案1】:

    那个输出是你的 ObstacleField 的内存地址。

    你必须迭代二维数组并打印每个位置:

    for (int i = 0; i < m.ObstacleField.length; i++) {
        for (int j = 0; j < m.ObstacleField[i].length; j++) {
            System.out.print(m.ObstacleField[i][j] + "\t");
        }
        System.out.println();
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用Arrays.deepToString 方法获取多维数组的String 表示:

      System.out.print(Arrays.deepToString(m.ObstacleField));
      

      输出:

      [[false, false, true], [false, false, true], [false, false, false]]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-07-28
        • 2016-08-26
        • 2013-02-25
        • 2016-04-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多