简体   繁体   English

Java中链接列表的链接列表

[英]Linked List of Linked Lists in Java

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的类)及其方法用于定义和其他add,get,listIterating操作,将会很有帮助。

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. YourClass对象的LinkedListLinkedList It can also be written in a simplified way since Java 7 : 它也可以从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子类)的子类,或者可以创建一个包含列表列表作为字段的类,并在其中添加方法以进行操作列表。

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 mainlist is LinkedList of LinkedLists and variable temp contains the value the first list stored ie sublist1 .. 这里变量mainlistLinkedLists的LinkedList ,变量temp包含第一个列表存储的值,即sublist1 ..

You can even simplify access to the secondary lists, eg 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. 此代码自动将新的LinkedList添加到外部列表中。 With this code you can later easily add single values: 使用此代码,您可以稍后轻松添加单个值:

lists.get(2).add("Foo");
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());

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

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