Java 初学者:如何将一个链表链接到另一个链表?
如果我有: linkedlist1= 1,2,3,4;
和 linkedlist2= 5,6,7;
我可以将 linkedlist2 附加到 linkedlist1 的末尾吗?如果我调用: linkedlist2.set(0,9999)
它会更改为 linkedlist2 = [999,6,7]
并且 linkedlist1
变为[1,2,3,4,9999,7,8];
?
这可能吗?或者我确实需要另一个结构?
以下代码不起作用:
List<Double> l1 = new LinkedList<Double>(Arrays.asList(1.0,2.0));
List<Double> l2 = new LinkedList<Double>(Arrays.asList(3.0,4.0));
l1.addAll(l2);
System.out.println(l1);
l2.set(0, 9.0);
System.out.println(l1);
输出:
[1.0, 2.0, 3.0, 4.0]
[1.0, 2.0, 3.0, 4.0]
If I have: linkedlist1= 1,2,3,4;
and linkedlist2= 5,6,7;
Am I able to attach linkedlist2 to the end of linkedlist1 in such a way if I invoke: linkedlist2.set(0,9999)
it changes to linkedlist2 = [999,6,7]
and linkedlist1
becomes [1,2,3,4,9999,7,8];
?
Is that possible ? Or I do need another structure ?
The following code didn't work:
List<Double> l1 = new LinkedList<Double>(Arrays.asList(1.0,2.0));
List<Double> l2 = new LinkedList<Double>(Arrays.asList(3.0,4.0));
l1.addAll(l2);
System.out.println(l1);
l2.set(0, 9.0);
System.out.println(l1);
OUTPUT:
[1.0, 2.0, 3.0, 4.0]
[1.0, 2.0, 3.0, 4.0]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
Java 提供的标准 LinkedList 类缺乏此功能。
正如 Donal Boyle 所发表的,您可以将一个列表的内容添加到另一个列表,但这不会维持您所描述的链接。
The standard LinkedList classes provided with Java lack this capability.
As Donal Boyle posts you can add the contents of one list to another but this doesn't maintain the linkage as your describe.
你不能,但作为替代方案,你可以有一个列表列表,但我不知道这是否可以满足你的需要:
You can't but as an alternative you can have a list of list, but I don't know if that would work as you need:
addAll()
只是从第二个链表中获取值。对第二个列表中的索引和设置值的更改不会影响第一个列表。 (注意:对对象本身的更改将保留,因为它们都指向同一个对象)。如果您正在寻找的话,您可以制作一个列表列表。
addAll()
just takes the values from the second linked list. Changes to the indexes and set values in the second list will not affect the first. (Note: changes to the objects themselves will stay because they both point to the same object).You can make a List of Lists if that is what you are looking for.
要回答有关更新一个列表并查看另一个列表中反映的更新的问题,答案是视情况而定。
如果您更新第一个列表中的对象,您将看到更改。但如果替换该对象,您将看不到变化。在第一个列表上调用 .set() 会替换列表中的对象。不幸的是,如果不替换原语(或其类表示形式,在本例中为 Float),则无法更新它的值。
要附加两个列表,假设您使用的是 java.util.LinkedList,您可以调用 addAll
To answer the question about updating one list and seeing the updates reflected in the other, the answer is it depends.
If you update an object in the first list you will see a change. but if you replace the object you will not see the change. Calling .set() on the first list is replacing the object in the list. Unfortunately, it is not possible to update the value of a primitive (or its class representation, in this case Float) without replacing it.
To append two lists, assuming you are using java.util.LinkedList, you can call addAll
实现此目的的最简单方法是为您的项目建立一个适当的类,并将该类的实例
MyInt
相互链接,而不是依赖 LinkedList,即添加一个MyInt next;< /code> 字段到保存整数的自定义类。
然后按照描述链接它们。
然后,甚至可以在不触及列表结构的情况下完成对一个实例的更改,并且在通过
next
引用迭代此列表时会相应地反映出来。The easiest way to achieve this is to have a proper class for your items and have instances of this class
MyInt
link to each other in stead of relying on LinkedList, i.e. add aMyInt next;
field to the custom class that holds your integers.Then link them as described.
A change to one instance can then be done without even touching the list structure and is reflected accordingly when iterating over this list through the
next
references.这个问题似乎是一个很好的应用程序 subList(int, int) 方法:
唉,subList 的 JavaDoc 指出:
因此,如果您想在 l1 上使用像
.add
这样的方法,那么您可能会运气不好。事实上,对于从AbstractList
派生的所有快速失败列表类(例如LinkedList
),subList 应该抛出ConcurrentModificationException< /code> 子列表的父列表被结构修改后。
但是,如果您远离父列表中的结构修改(或者在此类修改后重新创建子列表),那么一切都会按预期工作。
This problem seems to be a nice application for the subList(int, int) method:
Alas, the JavaDoc of subList states:
So if you want to use a method like
.add
on l1, then you may be out of luck. In fact, for all fail-fast list classes derived fromAbstractList
(such as asLinkedList
), subList should throw aConcurrentModificationException
after the parent list of a subList was structurally modified.However, if you stay away from structural modifications in the parent list (or if you re-create the sub-list after such modifications), then everything will work as expected.