Java 初学者:如何将一个链表链接到另一个链表? [英] Java Beginner: How do I link one linked list to another?
问题描述
如果我有:linkedlist1= 1,2,3,4;
和 linkedlist2= 5,6,7;
如果我调用:linkedlist2.set(0,9999)
,我是否可以将linkedlist2 附加到linkedlist1 的末尾:linkedlist2.set(0,9999)
它更改为linkedlist2 = [999,6,7]
和 linkedlist1
变成 [1,2,3,4,9999,7,8];
?
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 ?
以下代码无效:
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]
推荐答案
Java 提供的标准 LinkedList 类缺乏此功能.
The standard LinkedList classes provided with Java lack this capability.
当 Donal Boyle 发帖时,您可以将一个列表的内容添加到另一个列表中,但这不会像您描述的那样保持链接.
As Donal Boyle posts you can add the contents of one list to another but this doesn't maintain the linkage as your describe.
这篇关于Java 初学者:如何将一个链表链接到另一个链表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!