views:

56

answers:

3

Can I add a group of ArrayList into a single ArrayList? The ArrayList groups are of different sizes. Then How can I recognize each ArrayList?

+2  A: 

I hope I understand your question correctly.

An ArrayList is a list of Object types. If you wish to add an ArrayList as the item in your ArrayList, then you can do this.

An example

ArrayList list = new ArrayList();

ArrayList list2 = new ArrayList();
ArrayList list3 = new ArrayList();

list.add(list2);
list.add(list3);

This will result in your first ArrayList (list) containing two arraylists in position 0 and 1.

If however you are looking to add the contents of several ArrayLists to a single list of elements, then you use addAll on the ArrayList. Such as

ArrayList consolidatedList = new ArrayList();
list.addAll(list2);
list.addAll(list3);
Codemwnci
A: 

I didn't actually get what you want to do. Do you want to add all elements from different ArrayLists to one resulting ArrayList? If so, use ArrayList#addAll(Collection collection) method.

If you want an ArrayList of ArrayLists, use ArrayList#add(Object o) method. You can also parameterize your resulting ArrayList like this:

List<ArrayList> res = new ArrayList<ArrayList>()

folone
A: 

Yes you can add a "group of ArrayLists" into a single array list. If you want to "recognize" your ArrayLists, I would recommend using a Map, a HashMap for example.

Please also think about accepting answers to your other questions, and post code in your questions, you will get better help and people will hate you less.

Andrew