0

I am new to Java and was wondering what I did wrong. I am building a simple board game, but for some reason the array prints as an array of "Ljava.lang.String;@4bec1f0c". I've read some similar questions to this problem but for some reason, the solutions that I have found does not work in my code.

Help is appreciated.

for(int row = 0; row < gameBoard.length; row++)
          {
              for(int col = 0; col < gameBoard[row].length; col++)
              {                    
                    gameBoard[row][col] = "- ";
              }
          }
System.out.print(Arrays.toString(gameBoard));

2 Answers2

0

if you want to every object be visible in a 2-dimension array, use this method:

System.out.print(Arrays.deepToString(gameBoard));

Arrays.toString is working in single dimension arrays.

more data and examples: https://www.geeksforgeeks.org/arrays-deeptostring-in-java-with-example/

Majid Roustaei
  • 1,556
  • 1
  • 20
  • 39
0

Arrays.toString does the right thing for one-dimensional arrays, but won't help much on two-dimensional arrays.

Use Arrays.deepToString instead.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413