This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Java in General and the fly likes Help with Printing a 2D Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Help with Printing a 2D Array" Watch "Help with Printing a 2D Array" New topic
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: 19723
    
  20

Please SearchFirst next time. If you would have you would have found this thread.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
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: 19723
    
  20

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: 39478
    
  28
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Help with Printing a 2D Array