Reputation: 11
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
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