Java 初学者:如何将一个链表链接到另一个链表?

发布于 2024-10-13 05:56:11 字数 674 浏览 1 评论 0原文

如果我有: 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

浅笑轻吟梦一曲 2024-10-20 05:56:11

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.

晨曦÷微暖 2024-10-20 05:56:11

你不能,但作为替代方案,你可以有一个列表列表,但我不知道这是否可以满足你的需要:

import java.util.*;
import static java.lang.System.out;
class ListOfLists { 
  public static void main( String ... args ) { 

    List<Integer> a = new LinkedList<Integer>(Arrays.asList(1,2,3,4));
    List<Integer> b = new LinkedList<Integer>(Arrays.asList(5,6,7));  

    List<List> list = new LinkedList<List>();
    list.add( a );
    list.add( b );

    out.println("Before : " + list );
    b.set( 0, 999 );

    out.println("After  : " + list );

  }
}

Before : [[1, 2, 3, 4], [5, 6, 7]]
After  : [[1, 2, 3, 4], [999, 6, 7]]

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:

import java.util.*;
import static java.lang.System.out;
class ListOfLists { 
  public static void main( String ... args ) { 

    List<Integer> a = new LinkedList<Integer>(Arrays.asList(1,2,3,4));
    List<Integer> b = new LinkedList<Integer>(Arrays.asList(5,6,7));  

    List<List> list = new LinkedList<List>();
    list.add( a );
    list.add( b );

    out.println("Before : " + list );
    b.set( 0, 999 );

    out.println("After  : " + list );

  }
}

Before : [[1, 2, 3, 4], [5, 6, 7]]
After  : [[1, 2, 3, 4], [999, 6, 7]]
海未深 2024-10-20 05:56:11

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.

梦毁影碎の 2024-10-20 05:56:11

要回答有关更新一个列表并查看另一个列表中反映的更新的问题,答案是视情况而定。

如果您更新第一个列表中的对象,您将看到更改。但如果替换该对象,您将看不到变化。在第一个列表上调用 .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

杯别 2024-10-20 05:56:11

实现此目的的最简单方法是为您的项目建立一个适当的类,并将该类的实例 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 a MyInt 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.

与他有关 2024-10-20 05:56:11

这个问题似乎是一个很好的应用程序 subList(int, int) 方法:

List<Double> l1 = new LinkedList<Double>(Arrays.asList(1.0, 2.0, 3.0, 4.0));
List<Double> l2 = l1.subList(2, 4);

唉,subList 的 JavaDoc 指出:

返回的列表的语义
如果该方法变为未定义
支持列表(即此列表)是
以任何其他方式进行结构修改
比通过返回的列表。
(结构修改是那些
改变这个列表的大小,或者
否则以这种方式扰乱它
正在进行的迭代可能会产生
结果不正确。)

因此,如果您想在 l1 上使用像 .add 这样的方法,那么您可能会运气不好。事实上,对于从 AbstractList 派生的所有快速失败列表类(例如 LinkedList),subList 应该抛出 ConcurrentModificationException< /code> 子列表的父列表被结构修改后。

但是,如果您远离父列表中的结构修改(或者在此类修改后重新创建子列表),那么一切都会按预期工作。

This problem seems to be a nice application for the subList(int, int) method:

List<Double> l1 = new LinkedList<Double>(Arrays.asList(1.0, 2.0, 3.0, 4.0));
List<Double> l2 = l1.subList(2, 4);

Alas, the JavaDoc of subList states:

The semantics of the list returned by
this method become undefined if the
backing list (i.e., this list) is
structurally modified in any way other
than via the returned list.
(Structural modifications are those
that change the size of this list, or
otherwise perturb it in such a fashion
that iterations in progress may yield
incorrect results.)

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 from AbstractList (such as as LinkedList), subList should throw a ConcurrentModificationException 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文