1

When I print out the coding for

    //display the board
    public static void main(String[] args) {
            Scanner keyboard = new Scanner(System.in);
            Scanner scanner = new Scanner(System.in);
            char [][] gameboardTwo = {{'*', '*', 'S', 'T', 'A','R', '*', 'W', 'A', 'R', 'S', '*', '*', '*'}, {'*', '*', 'E','P', 'I', 'S', 'O', 'D', 'E', '*', '*', 'I', 'V', '*'}, {'*', '*', '*','*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*'}, {'*', '*', '*','*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*'}};
            System.out.print(gameboardTwo);
        }

}

all I get in return is this:

[[C@75b84c92
Process completed.
Virgo
  • 37
  • 1
  • 9
  • Not a huge Java expert but it looks like what is printing is the array reference and not the contents. See this other question. See: http://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-an-array – gnuchu Jan 29 '15 at 13:09

5 Answers5

10

A 2-D array can be printed using :

System.out.print(Arrays.deepToString(gameboardTwo));
Eran
  • 387,369
  • 54
  • 702
  • 768
1

Arrays are represented as Strings similarly to what the Object.toString method prints out, that is, with a type and a hash code.

To print your array, you must use the static utility methods of the Arrays class.

For instance: Arrays.toString(yourArray).

In your case, Arrays.deepToString(yourArray) since it has multiple dimensions.

Mena
  • 47,782
  • 11
  • 87
  • 106
1

If you use the System.out.println() Method in Java, it will print out the given Object by using its toString()Method.

In general this method just prints out a Text which uniquely identifies the Object. This standard implementation is where the

[[C@75b84c92

comes from in your example.

If you want to print out the entire content of your array you have two Options:

Either use the Arrays.toString() or Arrays.deepToString() methods. Or you iterate over the contents of the array and print out the data manually.

See What's the simplest way to print a Java array?

Community
  • 1
  • 1
jgroehl
  • 134
  • 6
0

something to remember about arrays is that they're a collection of items.

When you want to print out a collection of items, generally it makes sense to get each item in the array and print it out separate. So if you have a collection of strings, you iterate through each item in the array and print out the string.

if you have a collection of ints, objects, or other datatype, you can use the .toString() method of that datatype so that it can be turned into a string to print out. I think Java implicitly does that for certain data types when you try to print them out.

ex.

String[] myArray = {"a", "b", "c"};

void printArray() {
 for (String s : myArray) {
  System.out.print(s);
 }
 //or a traditional for loop, I forget how to get the length of 
 //an array in Java
 for (int i = 0; i < myArray.length; i++) {
  System.out.print(myArray[i]);
 }
 //in your case, multidimensional array, this should work
 //haven't tested it though
 String[][] myArray = {{"a","b"},{"c","d"}};
 for (int i = 0; i < myArray.length; i++) {
  for (int j = 0; j < myArray[i].length; j++) {
   System.out.print(myArray[i][j]);
  }
 }
}
Abdul Ahmad
  • 9,673
  • 16
  • 64
  • 127
0

For getting indivisual elements of the array follow below code :-

 public static void main(String[] args) {
         Scanner keyboard = new Scanner(System.in);
         Scanner scanner = new Scanner(System.in);
         char [][] gameboardTwo = {
                                      {'*', '*', 'S', 'T', 'A','R', '*', 'W', 'A', 'R', 'S', '*', '*', '*'}, 
                                      {'*', '*', 'E','P', 'I', 'S', 'O', 'D', 'E', '*', '*', 'I', 'V', '*'}, 
                                      {'*', '*', '*','*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*'}, 
                                      {'*', '*', '*','*', '*', '*', '*', '*', '*', '*', '*', '*', '*', '*'}
                                  };
         System.out.println(Arrays.deepToString(gameboardTwo));
         System.out.println("////////////////////////////");
         System.out.println(gameboardTwo[0].length);
         System.out.println("////////////////////////////");
         for(int i=0; i<4; i++){
             for(int j=0; j<14; j++){
                 System.out.println(gameboardTwo[i][j]);
             }
             System.out.println("**** row ends ****");
         }

     }