簡體   English   中英

java中二維arrays的Output,(取這個錯誤[[Z@6d06d69c)

[英]Output of 2-dimensional arrays in java , (take this error [[Z@6d06d69c )

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);

    }
}

嗨,首先我閱讀了有關該問題的所有問題,但找不到答案,我仍然得到同樣的錯誤。 通常我知道二維 boolean arrays 默認為假,但我無法得到這樣的 output。 什么問題,你能幫忙嗎?

我拿這個 output:

[[Z@6d06d69c

output 是您的 ObstacleField 的 memory 地址。

您必須迭代二維數組並打印每個 position:

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();
}

您可以使用Arrays.deepToString方法來獲取多維數組的String表示:

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

Output:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM