简体   繁体   English

Java列表清单

[英]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: 我是Java编程的新手,我试图从列表中获取元素并将其作为单例列表放入超列表中:

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

How do I do this? 我该怎么做呢?

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: 其中Object是您要放入列表中的对象的类型,例如String对象,那么您只需编写:

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: 接下来,您可以使用list2来访问主列表中的列表元素,例如:

Object o = list2.get(index);

etc 等等

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM