java中二维数组的输出

bnlyeluc  于 2021-07-09  发布在  Java
关注(0)|答案(2)|浏览(496)
  1. public class Main {
  2. boolean[][] ObstacleField; // board with mines marked as true
  3. int rows; // number of rows of the board
  4. int cols; // number of columns of the board
  5. int numObstacle;
  6. Main(int rows, int cols, int numMines) {
  7. this.rows = rows;
  8. this.cols = cols;
  9. this.numObstacle = num;
  10. this.ObstacleField = new boolean[rows][cols];
  11. Board();
  12. }
  13. public void Board() {
  14. int i = 0, j = 0;
  15. Random r = new Random();
  16. for (i = 0; i < this.numObstacle; ) {
  17. int x = r.nextInt(this.cols);
  18. int y = r.nextInt(this.rows);
  19. if (ObstacleField[x][y] != true) {
  20. ObstacleField[x][y] = true;
  21. i++;
  22. }
  23. }
  24. }
  25. public static void main(String[] args) {
  26. Main m = new Main(3, 3, 2);
  27. System.out.print(m.ObstacleField);
  28. }
  29. }

嗨,首先我看了所有关于这个问题的问题,但是我找不到答案,我还是得到了同样的错误。通常我知道二维布尔数组在默认情况下是假的,但是我不能得到这样的输出。有什么问题,你能帮忙吗?
我把这个输出:

  1. [[Z@6d06d69c
2wnc66cl

2wnc66cl1#

你可以用 Arrays.deepToString 获取 String 多维数组的表示:

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

输出:

  1. [[false, false, true], [false, false, true], [false, false, false]]
3phpmpom

3phpmpom2#

该输出是obstaclefield的内存地址。
您必须迭代二维数组并打印每个位置:

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

相关问题