java Java中链表的链表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11050407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Linked List of Linked Lists in Java
提问by Anand Kumar
I would like to know how to create a linked list of linked lists. Also, It would be helpful if the predefined LinkedList
(class from Java) and its methods are used for defining and for other add, get, listIterating operations.
我想知道如何创建链表的链表。此外,如果预定义的LinkedList
(来自 Java 的类)及其方法用于定义和其他添加、获取、列表迭代操作,这将很有帮助。
回答by Autar
You can put any object in a list, including another list.
您可以将任何对象放在一个列表中,包括另一个列表。
LinkedList<LinkedList<YourClass>> list = new LinkedList<LinkedList<YourClass>>();
is a LinkedList
of LinkedList
s of YourClass
objects. It can also be written in a simplified way since Java 7:
是LinkedList
的LinkedList
第YourClass
对象。从 Java 7 开始,它也可以用一种简化的方式编写:
LinkedList<LinkedList<YourClass>> list = new LinkedList<>();
Very simple examples of manipulating such a list:
操作此类列表的非常简单的示例:
You then need to create each sublist, here adding a single sublist:
然后您需要创建每个子列表,这里添加一个子列表:
list.add(new LinkedList<YourClass>());
Then create the content objects:
然后创建内容对象:
list.get(sublistIndex).add(new YourClass());
You can then iterate over it like this (sublists' items are grouped by sublist):
然后你可以像这样迭代它(子列表的项目按子列表分组):
for(LinkedList<YourClass> sublist : list) {
for(YourClass o : sublist) {
// your code here
}
}
If you want to add specific methods to this list of lists, you can create a subclass of LinkedList
(or List
, or any other List
subclasses) or you can create a class with the list of lists as a field and add methods there to manipulate the list.
如果要将特定方法添加到此列表列表中,您可以创建LinkedList
(或List
,或任何其他List
子类)的子类,或者您可以创建一个将列表列表作为字段的类,并在那里添加方法来操作列表。
回答by Arjun K P
Well i've done this code and i've got it right
好吧,我已经完成了这段代码,而且我做对了
java.util.LinkedList mainlist = new java.util.LinkedList();
java.util.LinkedList sublist1 = new java.util.LinkedList();
sublist1.add(object1);
sublist1.add(object2);
sublist1.add(object3);
java.util.LinkedList sublist2=new java.util.LinkedList();
sublist2.add(1);
sublist2.add(2);
mainlist.add(sublist1);
mainlist.add(sublist2);
// To retrieve the sublist1 from mainlist...........
java.util.LinkedList temp = (java.util.LinkedList)mainlist.get(0);
Here variable mainlistis LinkedList of LinkedListsand variable tempcontains the value the first list stored i.e sublist1..
这里变量mainlist是LinkedLists 的 LinkedList变量temp包含存储的第一个列表的值,即sublist1..
回答by Moritz Petersen
You can even simplify access to the secondary lists, e.g. using
您甚至可以简化对二级列表的访问,例如使用
final List<List<String>> lists = new LinkedList<List<String>>() {
@Override
public List<String> get(final int index) {
while (index >= size()) {
add(new LinkedList<>());
}
return super.get(index);
}
};
This code automatically adds new LinkedList
s to the outer list. With this code you can later easily add single values:
此代码自动将 new LinkedList
s添加到外部列表。使用此代码,您以后可以轻松添加单个值:
lists.get(2).add("Foo");
回答by SetSlapShot
LinkedList<LinkedList<YourClass>> yourList = new LinkedList<LinkedList<YourClass>>();
As the declaration. To add another linked list (to the end by default) you would do
作为声明。要添加另一个链接列表(默认情况下到最后),您会这样做
yourList.add(new LinkedList<YourClass>());
To add an element to lets say the second linked list in the series:
要添加一个元素,让我们说系列中的第二个链表:
yourList.get(1).add(new YourClass());