简体   繁体   English

将ArrayList添加到另一个ArrayList

[英]Adding An ArrayList To Another ArrayList

I am currently studying for the Java Programmer 1 certificate and the following piece of code came up about adding an ArrayList to another. 我目前正在研究Java程序员1证书 ,下面的代码片段是有关将ArrayList添加到另一个证书的。

ArrayList<String> myArrList = new ArrayList<String>();
            myArrList.add("One");
            myArrList.add("Two");
            ArrayList<String> yourArrList = new ArrayList<String>();
            yourArrList.add("Three");
            yourArrList.add("Four");
            myArrList.addAll(1, yourArrList); 
            for (String val : myArrList)
             System.out.println(val);

This is what the author then says: 这就是作者的话:

What happens if you modify the common object references in these lists, myArrList and yourArrList? 如果您修改这些列表myArrList和yourArrList中的公共对象引用,会发生什么情况? We have two cases here: 这里有两种情况:

In the first one, you reassign the object reference using either of the lists. 在第一个中,使用两个列表之一重新分配对象引用。 In this case, the value in the second list will remain unchanged. 在这种情况下,第二个列表中的值将保持不变。

In the second case, you modify the internals of any of the common list elements,in this case, the change will be reflected in both of the lists. 在第二种情况下,您将修改任何公共列表元素的内部,在这种情况下,更改将反映在两个列表中。

What is the author trying to say?? 作者想说什么? I am a bit confused about the 2 cases he mentions! 我对他提到的两个案例感到有些困惑!

Any help would be greatly appreciated. 任何帮助将不胜感激。

I think I know what the author is trying to say. 我想我知道作者想说什么。 But Strings are a bad example to do it with. 但是字符串是一个不好的例子。 Imagine something like this. 想象一下这样的事情。 He is explaining the difference between adding two different instances of a class to the lists, or adding the same instance to both lists. 他正在解释将类的两个不同实例添加到列表,或将相同实例添加到两个列表之间的区别。 When you add the same instance to both lists, if you modify that instance the change is reflected in both lists. 当您将相同实例添加到两个列表中时,如果修改该实例,更改将反映在两个列表中。

import java.util.ArrayList;

public class Example {

    private static class Node {
        private int value;

        public Node(final int value) {
            this.value = value;
        }

        public int getValue() {
            return value;
        }

        public void setValue(final int value) {
            this.value = value;
        }
    }

    public static void main(final String... args) {
        final ArrayList<Node> nodes1 = new ArrayList<>();
        final ArrayList<Node> nodes2 = new ArrayList<>();

        // add two different Node objects that happen to have same value
        nodes1.add(new Node(1337));
        nodes2.add(new Node(1337));

        Node node = new Node(69);

        // add the same node to both lists
        nodes1.add(node);
        nodes2.add(node);

        node.setValue(420);

        // do your join here and print result to see {1337, 420, 1337, 420}
        nodes1.addAll(0, nodes2);
        for (final Node n : nodes1)
            System.out.println(n.getValue());
    }

}

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

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