Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

expandable multidimensional ArrayList or Multidimensional Arrays?

 
Vladimir Korabelnikoff
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20196
26
MySQL Database
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47293
52
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20388
46
Chrome Eclipse IDE Java Windows
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Collections can hold any objects, and that includes other collections. At least if you use the right generic type:
 
Vladimir Korabelnikoff
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47293
52
  • 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 3742
16
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vladimir Korabelnikoff
Greenhorn
Posts: 10
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9490
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Joanne Neal
Rancher
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic