user2117619
user2117619

Reputation: 11

List of list in Java

I'm very new to Java programming and I'm trying to take elements from a list and put them into a superlist as singleton lists:

[object1,object2,object3] ---> [[object1][object2][object3]]

How do I do this?

Upvotes: 0

Views: 90

Answers (1)

Kakalokia
Kakalokia

Reputation: 3191

If you want to create a list of lists, you can do something like:

List<List<Object>> list = new ArrayList<List<Object>>();

Where Object is the type of object you want to put in your list, for example a String object, then you'd just write:

List<List<String>> list = new ArrayList<List<String>>();

If you wonder "How can I access the elements from the list inside the main list", then first you know to access the list inside your main list like:

List list2 = list.get(index);

Next you can use list2 to access the elements of the list inside your main list like:

Object o = list2.get(index);

etc

Upvotes: 5

Related Questions