aspose file tools*
The moose likes Beginning Java and the fly likes expandable multidimensional ArrayList or Multidimensional Arrays? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "expandable multidimensional ArrayList or Multidimensional Arrays?" Watch "expandable multidimensional ArrayList or Multidimensional Arrays?" New topic
Author

expandable multidimensional ArrayList or Multidimensional Arrays?

Vladimir Korabelnikoff
Greenhorn

Joined: Oct 21, 2011
Posts: 10
So I need to be able to store data from an xml file in a collection.

The output is the equivalent of a two-dimensional table.

I need to be able to EXPAND it, so Array is almost not an option.

Would I be better off trying to use a multidimensional arraylist?

Or, would I be better off creating a new array (and copying the other array to the new one) every time I add an element to it?


Please let me know ;) Thanks guys!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18657
    
    8

Vladimir Korabelnikoff wrote:Or, would I be better off creating a new array (and copying the other array to the new one) every time I add an element to it?


But that's basically what an ArrayList does. So if that's what you need then use an ArrayList.
Vladimir Korabelnikoff
Greenhorn

Joined: Oct 21, 2011
Posts: 10
Paul Clapham wrote:
Vladimir Korabelnikoff wrote:Or, would I be better off creating a new array (and copying the other array to the new one) every time I add an element to it?


But that's basically what an ArrayList does. So if that's what you need then use an ArrayList.


Thanks a bunch! I'll look into how to make ArrayLists's multidimensional!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
There is no such thing as a multidimensional array, only an array of arrays. There is no such thing as a multidimensional ArrayList, but you can have a List of Lists.
Vladimir Korabelnikoff
Greenhorn

Joined: Oct 21, 2011
Posts: 10
Campbell Ritchie wrote:There is no such thing as a multidimensional array, only an array of arrays. There is no such thing as a multidimensional ArrayList, but you can have a List of Lists.


Oh Really? That's awesome! Can you put any type of Collection inside of a Collection? Or is it just Lists?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19720
    
  20

Collections can hold any objects, and that includes other collections. At least if you use the right generic type:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Vladimir Korabelnikoff
Greenhorn

Joined: Oct 21, 2011
Posts: 10
Rob Spoor wrote:Collections can hold any objects, and that includes other collections. At least if you use the right generic type:


Thanks for that. I created an ArrayList of ArrayLists.

However, I have a nested for loop where I read my XML, and I have no idea how to name the sub-Lists in the for loop.


That's alright for the first loop, but what happens when it comes back a second time? singleLists should be renamed, if I'm correct.

I'm just not sure how :l Thanks for your help, guys.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39409
    
  28
You ought to have declared it as Rob told you. You can iterate a List<List<Foo>> in a for-each loop, with each element found being a List<Foo>. Like this:
Vladimir Korabelnikoff
Greenhorn

Joined: Oct 21, 2011
Posts: 10
Campbell Ritchie wrote:You ought to have declared it as Rob told you. You can iterate a List<List<Foo>> in a for-each loop, with each element found being a List<Foo>. Like this:


Awesome! Thank you guys so much! You are legends! I have gotten it to work. I now read the XML, and put into a List of Lists, then return that back to the main class.

Now I have to figure out how to sort through them (by certain fields)/

This is the XML:



I need to be able to order it by date (ascending, and descending). The date is of course, in UNIX timestamp.

Note that I'm not sorting XML, I'm sorting through the List of Lists.

Can you guy please point me in the right direction? Thanks so much for your help.

Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3656
    
  15
Have you been told to use a list of lists ?
The better solution is to create a Company class and an Employee class and the Company class will contain a list of Employees.

Off topic - your list of employees reminds me of a Monty Python sketch.


Joanne
Vladimir Korabelnikoff
Greenhorn

Joined: Oct 21, 2011
Posts: 10
Joanne Neal wrote:Have you been told to use a list of lists ?
The better solution is to create a Company class and an Employee class and the Company class will contain a list of Employees.

Off topic - your list of employees reminds me of a Monty Python sketch.


I'm sure I have been told to do lists, and that's what I'm using? Maybe I missed something.

I'm not actually sure what you mean by having a class for companies and employees. I only need one company, and multiple employees.

Edit: Haha, why so? I've only seen one Monty Python sketch (The Holy Grail) - The funniest stuff ever.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 8008
    
  22

Vladimir Korabelnikoff wrote:I'm not actually sure what you mean by having a class for companies and employees. I only need one company, and multiple employees.

So, you have a Company class and an Employee class; and inside your Company class, you have a member field something like:

private List<Employee> employees = new ArrayList<Employee>();

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3656
    
  15
Vladimir Korabelnikoff wrote:Edit: Haha, why so? I've only seen one Monty Python sketch (The Holy Grail) - The funniest stuff ever.

Monty Python Bruce sketch
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: expandable multidimensional ArrayList or Multidimensional Arrays?