简体   繁体   English

如何添加一个ArrayList <Integer> 到ArrayList <ArrayList<Integer> &gt;一次又一次地使用ArrayList中的不同元素 <Integer>

[英]How to add an ArrayList<Integer> to an ArrayList<ArrayList<Integer>> again and again with different elements in ArrayList<Integer>

I have created an ArrayList of ArrayList in java. 我在Java中创建了ArrayList的ArrayList。 And iam storing values in them as follows : 并且iam将值存储在它们中,如下所示:

import java.util.ArrayList;


    class Test {
        public static void main(String args[]) {
                ArrayList<ArrayList<Integer>> connections = new ArrayList<ArrayList<Integer>>();
                ArrayList<Integer> currentConnects = new ArrayList<>();
                currentConnects.add(7);
                currentConnects.add(4);
                connections.add(currentConnects);
                currentConnects.clear();
                currentConnects.add(10);
                currentConnects.add(15);
                connections.add(currentConnects);
                System.out.println(connections);
        }
    }

I am expecting the output to be : 我期望输出为:

[[7, 4], [10, 15]]

but the output is : 但输出是:

[[10, 15], [10, 15]]

How can i achieve my expected output without using any other extra variables? 如何在不使用任何其他额外变量的情况下达到预期的输出?

You're actually referencing the same object twice in your connections object, hence the result you have. 实际上,您在connections对象中两次引用同一对象,因此得到的结果是。

Instead of currentConnects.clear(); 代替currentConnects.clear(); try currentConnects = new ArrayList<>(); 尝试currentConnects = new ArrayList<>();

You need to remove currentConnects.clear(); 您需要删除currentConnects.clear();。 and create a new ArrayList Object.Your ArrayList objects are pointing to the same Object in heap.The ArrayList<ArrayList<Integer>> contains same reference of ArrayList twice 并创建一个新的ArrayList对象。您的ArrayList对象指向堆中的同一对象。ArrayList ArrayList<ArrayList<Integer>>两次包含ArrayList的相同引用

import java.util.ArrayList; 导入java.util.ArrayList;

    class Test {
        public static void main(String args[]) {
                ArrayList<ArrayList<Integer>> connections = new ArrayList<ArrayList<Integer>>();
                ArrayList<Integer> currentConnects = new ArrayList<>();
                currentConnects.add(7);
                currentConnects.add(4);
                connections.add(currentConnects);

                currentConnects = new ArrayList<>();
                currentConnects.add(10);
                currentConnects.add(15);
                connections.add(currentConnects);
                System.out.println(connections);
        }
    }

You have to create new array lists everytime. 您必须每次都创建新的数组列表。

When you do: 当您这样做时:

  connections.add(currentConnects);
  currentConnects.clear();

You first add a reference of currentConnects, and then clear the content of currentConnects, you are not creating a new arraylist. 您首先添加currentConnects的引用,然后清除currentConnects的内容,而不是在创建新的数组列表。 Instead do: 而是:

  ArrayList<Integer> currentConnects = new ArrayList<>();
  currentConnects.add(7);
  currentConnects.add(4);
  connections.add(currentConnects);
  currentConnects = new ArrayList<>();
  currentConnects.add(10);
  currentConnects.add(15);
  connections.add(currentConnects);

When you call connections.add(currentConnects); 当您调用connections.add(currentConnects); , since connections is of type ArrayList<ArrayList<Integer>> , you're actually adding a reference to entire ArrayList connections, NOT just the integers in the ArrayList itself. 由于connections的类型为ArrayList<ArrayList<Integer>> ,实际上是增加了整个ArrayList中连接的参考, 而不是仅仅在ArrayList中本身的整数。

So because you call connections.add(currentConnects); 因此,因为您调用connections.add(currentConnects); twice, there are two references to connections ArrayList, which is why the integers held in connections are printed out twice. 两次,有两个对connections ArrayList的引用,这就是为什么保留在connections中的整数打印两次的原因。

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

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