gpt4 book ai didi

Java初学者: How do I link one linked list to another?

转载 作者:行者123 更新时间:2023-12-01 16:40:05 28 4
gpt4 key购买 nike

如果我有: linkedlist1= 1,2,3,4;linkedlist2= 5,6,7;

如果我调用: 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]

最佳答案

Java 提供的标准 LinkedList 类缺乏此功能。

正如 Donal Boyle 所发的帖子,您可以将一个列表的内容添加到另一个列表中,但这不会保持您所描述的链接。

关于Java初学者: How do I link one linked list to another?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4726160/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com