Java初学者:如何将一个链表链接到另一个? [英] Java Beginner: How do I link one linked list to another?

查看:150
本文介绍了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];

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆