• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

expandable multidimensional ArrayList or Multidimensional Arrays?

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Sheriff
Posts: 28395
100
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Marshal
Posts: 80634
471
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 80634
471
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic