| Author |
Help with Printing a 2D Array
|
Joanna Spence
Greenhorn
Joined: May 31, 2010
Posts: 23
|
|
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
Joined: Oct 27, 2005
Posts: 19232
|
|
|
Please SearchFirst next time. If you would have you would have found this thread.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Joanna Spence
Greenhorn
Joined: May 31, 2010
Posts: 23
|
|
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
Joined: Oct 27, 2005
Posts: 19232
|
|
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
Joined: Oct 13, 2005
Posts: 32830
|
|
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.
|
 |
 |
|
|
subject: Help with Printing a 2D Array
|
|
|