简体   繁体   English

双链表

[英]doubly-linked list

Hi I want to know that how can I copy my objects from an arrayList to a doubly linked list? 嗨,我想知道如何将我的对象从arrayList复制到双向链接列表? also my DNode constructor is : 我的DNode构造函数也是:

    public DNode(Object element, DNode prev, DNode next) {
    this.element = element;
    this.next = next;
    this.prev = prev;
}

ie when I write such a code my program doesn't work : 即当我编写这样的代码时,我的程序不起作用:

  DNode node = new DNode(pointList.get(0),null, null);

        for (int i = 1; i < pointList.size(); i++) {
        DNode dNode = new DNode(pointList.get(i), node, null);
        dList.addLast(dNode);
        }

also i have written doubly linked list which has addAfter and addBefore methods and also much more. 我也写了双重链表,其中有addAfter和addBefore方法,还有更多。

java.util.LinkedList is a doubly-linked list. java.util.LinkedList是一个双向链接列表。

All of the operations perform as could be expected for a doubly-linked list. 所有操作均按双向链表的预期执行。

You can create it by passing the array list as constructor argument: 您可以通过将数组列表作为构造函数参数传递来创建它:

List linkedList = new LinkedList(arrayList);

Update: The java.util.LinkedList has add(index, element) which, combined with indexOf(..) should cover the addBefore and addAfter methods. 更新: java.util.LinkedList具有add(index, element) ,该add(index, element)indexOf(..)组合起来应该覆盖addBeforeaddAfter方法。 You can extend LinkedList to add these convenient methods if you like. 如果愿意,可以扩展LinkedList以添加这些方便的方法。

Assuming the element at the end of the linked list has a 'next' property of 0: 假设链表末尾的元素的“ next”属性为0:

ArrayList arrayList = new ArrayList();
int next = currentElement.next;
while(next != 0) {
    arrayList.add(currentElement);
    next = currentElement.next;
}

You can also use java.util.LinkedList as that is an in-built representation of a doubly-linked list. 您还可以使用java.util.LinkedList因为它是双向链接列表的内置表示形式。 Using this type means you can pass the linked list into the constructor of an ArrayList 使用此类型意味着您可以将链接列表传递到ArrayList的构造函数中

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

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