双向链表

发布于 2024-10-04 16:10:10 字数 536 浏览 2 评论 0原文

你好 我想知道如何将对象从 arrayList 复制到双向链表? 我的 DNode 构造函数也是:

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

即当我编写这样的代码时,我的程序无法工作:

  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);
        }

我还编写了双向链表,其中包含 addAfter 和 addBefore 方法等等。

Hi
I want to know that how can I copy my objects from an arrayList to a doubly linked list?
also my DNode constructor is :

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

i.e. 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

左岸枫 2024-10-11 16:10:10

java.util.LinkedList 是一个双向链表。

所有操作都按照双向链表的预期执行。

您可以通过将数组列表作为构造函数参数传递来创建它:

List linkedList = new LinkedList(arrayList);

更新:java.util.LinkedList具有add(index, element),其中,与 indexOf(..) 结合应覆盖 addBeforeaddAfter 方法。如果您愿意,您可以扩展LinkedList来添加这些方便的方法。

java.util.LinkedList is a doubly-linked list.

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. You can extend LinkedList to add these convenient methods if you like.

吝吻 2024-10-11 16:10:10

假设链表末尾的元素的“next”属性为 0:

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

您还可以使用 java.util.LinkedList,因为它是双向链表的内置表示。使用此类型意味着您可以将链表传递到 ArrayList 的构造函数中

Assuming the element at the end of the linked list has a 'next' property of 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. Using this type means you can pass the linked list into the constructor of an ArrayList

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文