• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Help with Printing a 2D Array

 
Joanna Spence
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm building an adjacency matrix based on entries from three different integer ArrayLists.
I want to print the 2d array as a grid format...and
I'm getting an output of


I think maybe I'm doing something wrong..

here is my method to build the 2d array:

It's based on Vertices and Edges, with Distance (to eventually build an MST, and return shortest paths based on distances)



then in my main method, i call the adjacency building method

like this


and then I called to print the returned 2d Array
like this:



and I got this as the print out:

[[I@360be0


Any ideas?

 
Rob Spoor
Sheriff
Pie
Posts: 20559
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please SearchFirst next time. If you would have you would have found this thread.
 
Joanna Spence
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok so I did what the thread suggested..sorry I should have searched first by the way.

So it said to use the toString method...i tried that, and it gave me a printout of the same characters which it says in the thread that they are hashcodes,
but it printed multiples equal to the size of the 2D array which is 9.

So it talks about overriding the hashcode...i need a little more direction here.

 
Rob Spoor
Sheriff
Pie
Posts: 20559
57
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't override hashCode for arrays.

The problem with Arrays.toString(Object[])* is that it calls toString() on its elements -- and toString() still returns something useless for arrays. The good news is that that method mentions a good replacement: deepToString(Object[]). Check out the following:

* an int[] is an Object, so an int[][] is an Object[].
 
Campbell Ritchie
Sheriff
Pie
Posts: 49472
64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote: . . . elements -- and toString() still returns something useless for arrays . . .
What Rob is hinting at, is there is no such thing as a 2D array. Only an array of arrays.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic