• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help with Printing a 2D Array

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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[].
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
If you open the box, you will find Heisenberg strangling Shrodenger's cat. And waving this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic