开发者

Can I add a group of ArrayList into a single ArrayList?

Can I add 开发者_Python百科a group of ArrayList into a single ArrayList? The ArrayList groups are of different sizes. Then How can I recognize each ArrayList?


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);


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>()


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜