aspose file tools*
The moose likes Beginning Java and the fly likes  storeing 2d array in arraylist? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark " storeing 2d array in arraylist?" Watch " storeing 2d array in arraylist?" New topic
Author

storeing 2d array in arraylist?

sudde gameeef
Greenhorn

Joined: Mar 21, 2013
Posts: 19
how to save 2d array in arraylist and how to print it???


it print the wrong stuff. try running it to see the problem.
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2861
    
  11

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

Joined: Mar 21, 2013
Posts: 19
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

Joined: Oct 01, 2001
Posts: 2861
    
  11

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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: storeing 2d array in arraylist?