简体   繁体   English

将LinkedList添加到包含LinkedList的LinkedList中,并更改添加的LinkedList

[英]Adding a LinkedList to a LinkedList that contains LinkedLists and changing the added LinkedList

I want to add a LinkedList<Integer> (let's call it list A) to a LinkedList<LinkedList<Integer>> (call it list B). 我想向LinkedList<LinkedList<Integer>> (称为列表B)添加LinkedList<Integer> (我们将其称为列表A)。 After doing so I need to change the values of list A and add it to list B again, but without changing the values already stored in List B. 这样做之后,我需要更改列表A的值,然后再次将其添加到列表B,但不更改已存储在列表B中的值。

I need this to store a unknown number of paths in a LinkedList containing LinkedLists. 我需要它在包含LinkedLists的LinkedList中存储未知数量的路径。 The number of LinkedList's that are supposed to be added to List B are always different and I cannot just work with a LinkedList<Integer> C that copies A. In most cases I'm probably going to need a lot of list's in list B. 应该添加到列表B的LinkedList的数量始终是不同的,并且我不能仅使用复制A的LinkedList<Integer> C来工作。在大多数情况下,列表B中可能需要很多列表。

public static void main(String[] args) 
{
    LinkedList<LinkedList<Integer>> B = new LinkedList<LinkedList<Integer>>();
    LinkedList<Integer> A = new LinkedList<Integer>();

    // just adding some numbers to List A
    A.add(1);
    A.add(2);
    A.add(3);

    // adding list A to list B
    B.add(A);

    // adding another number to list A
    A.add(4);

    // this will print out [[1,2,3,4]] now
    // I want it to print out [[1,2,3]], even though I changed List A
    System.out.println(B);
}

Current result: 当前结果:

[[1, 2, 3, 4]];

Expected result: 预期结果:

[[1,2,3]]

You can do something like this, 你可以做这样的事情,

 A = new LinkedList<Integer>();
 A.add(4);

The code why it's behaving like that is You are adding the reference of linkedlist a to b so whatever changes you made to a will reflect in the b. 其行为方式的代码是您正在将链表a的引用添加到b,因此您对a所做的任何更改都将反映在b中。

The below code will copy all contents of the previous list stored into B, Via Collections.copy, and leave you free to change the contents of List A without affecting the values of the lists stored in B. 下面的代码将通过Via Collections.copy将存储在先前列表中的所有内容复制到B中,使您可以随意更改列表A的内容,而不会影响存储在B中的列表的值。

public static void main(String[] args) {
    LinkedList<LinkedList<Integer>> B = new LinkedList<LinkedList<Integer>>();
    for (int i = 0; i < listsGiven.size(); i++) {
        LinkedList<Integer> A = listsGiven.get(i);
        successiveCopy(B, A, i);
    }
}

static void successiveCopy(LinkedList<LinkedList<Integer>> B, LinkedList<Integer> A, int index) {
    if (B.size() == 0) { 
        B.add(A); return; 
    }
    B.add(Collections.copy(A, B.get(index - 1));
}

Personally, I would copy when adding to the outer list. 就个人而言,我将在添加到外部列表时进行复制。

B.add(new LinkedList<>(A));

This way the copying is coupled with the reason that the copying is needed: you want to store the current state of the list. 这种复制方式与需要复制的原因结合在一起:您要存储列表的当前状态。 You can then safely modify the original list. 然后,您可以安全地修改原始列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM