双向链表
你好 我想知道如何将对象从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
java.util.LinkedList
是一个双向链表。您可以通过将数组列表作为构造函数参数传递来创建它:
更新:
java.util.LinkedList
具有add(index, element)
,其中,与indexOf(..)
结合应覆盖addBefore
和addAfter
方法。如果您愿意,您可以扩展LinkedList
来添加这些方便的方法。java.util.LinkedList
is a doubly-linked list.You can create it by passing the array list as constructor argument:
Update: The
java.util.LinkedList
hasadd(index, element)
which, combined withindexOf(..)
should cover theaddBefore
andaddAfter
methods. You can extendLinkedList
to add these convenient methods if you like.假设链表末尾的元素的“next”属性为 0:
您还可以使用 java.util.LinkedList,因为它是双向链表的内置表示。使用此类型意味着您可以将链表传递到 ArrayList 的构造函数中
Assuming the element at the end of the linked list has a 'next' property of 0:
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