• 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

storeing 2d array in arraylist?

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how to save 2d array in arraylist and how to print it???


it print the wrong stuff. try running it to see the problem.
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

Line 18 won't compile right? So it's not really printing anything.

You've declared your ArrayList to contain 2D arrays of Integers, but then you write code that seems to want to store individual ints in it, and even that's not done exactly right.

I'd start by changing line 6 to:


Then see if you can work that compile error out of there.
 
sudde gameeef
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i jave some question:
1st i am not sure but i hear that when we store 2d array in to list than we dont need loops?
2nd should i used 2d arraylist bc i am using 2d int array?

-------------------------------------------------------------
error is gone now. how can i print the arraylist:
i did this and i think its printing the index values


 
Greg Charles
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The thing is an Integer is an Object, and a 2D array of Integers taken as a whole is also an Object, just a different kind. You have to decide if you want the elements of the list to be the individual Integers in your array (in your case this is 90 elements), or do you want the elements to be the whole array, so the list only has one element. In the first case, you'd want loops to populate the list, but in the second, you wouldn't. Which form you use depends on your assignment, or in the real word, whichever form will be more useful to the rest of your application.

Printing out a 2D array is going to give you something like: [[Ljava.lang.Integer;@1c78e57 That's probably not what you want, so you will likely need loops to print it out in the form you prefer.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic