ArrayList中的嵌套ArrayList在Java中。

huangapple go评论84阅读模式
英文:

ArrayList inside an ArrayList in java

问题

我在使用Java时遇到了一个问题,需要使用嵌套的ArrayList,并且在每次迭代后都必须清除内部列表。我注意到,当我将内部列表添加到主列表中并稍后清除内部列表时,主列表的值也发生了变化。是否有人可以解释一下这为什么会发生?另外,如果我使用数组而不是ArrayList,是否会有任何变化?我已经附上了我的问题的代码如下:

import java.util.*;
import java.lang.*;
import java.io.*;

class Example
{
    public static void main(String []args){
        ArrayList<ArrayList<Integer>> list= new ArrayList<>();
        ArrayList<Integer> innerlist = new ArrayList<>();
        innerlist.add(1);
        list.add(innerlist);
        System.out.println(list);
        innerlist.clear();
        System.out.println(list);
    }
}

输出:

[[1]]
[[]]
英文:

I came across a problem in which I had to use nested ArrayList in java and had to clear the innerlist after each iteration. I noticed that when I added the innerlist to the main list and cleared the innerlist later , the value of the main list changed . Can anyone please explain why this is happening . Also , will there be any change if I use array instead of ArrayList. I have attached the code to my problem below to make it clear.

import java.util.*;
import java.lang.*;
import java.io.*;

class Example
{


     public static void main(String []args){
        
        ArrayList&lt;ArrayList&lt;Integer&gt;&gt; list= new ArrayList&lt;&gt;();
        ArrayList&lt;Integer&gt; innerlist = new ArrayList&lt;&gt;();
        innerlist.add(1);
        list.add(innerlist);
        System.out.println(list);
        innerlist.clear();
        System.out.println(list);
}
 
}

Output :
[1]
[[]]

答案1

得分: 0

innerlist 仍然指向 ArrayList。

查看这个链接:
https://stackoverflow.com/questions/184710/what-is-the-difference-between-a-deep-copy-and-a-shallow-copy

英文:

innerlist is pointing still pointing to the ArrayList.

Check this:
https://stackoverflow.com/questions/184710/what-is-the-difference-between-a-deep-copy-and-a-shallow-copy

答案2

得分: 0

这是因为 add 操作并不会复制任何内容 - 它只是将你传递给它作为参数的对象的引用添加到列表的末尾。如果你希望对 innerlist 引用的更改不会影响到你的 list,你需要对其进行复制,例如使用接受集合参数的构造函数。你可以在添加时进行如下操作:

list.add(new ArrayList<>(innerlist));

或者在添加后重新将 innerlist 赋值为一个新的列表:

list.add(innerlist);
innerlist = new ArrayList<>();

特别是在这一点上,如果你需要清空它的话。
另请参阅这个问题,它处理了相同的问题,但没有使用外部列表。

英文:

This is happening because add doesn't copy anything - it just adds a reference to the object you pass as parameter to the end of the list. If you want that changes to the innerlist reference don't affect your list at all, you need to copy it, for example using the constructor taking a Collection parameter. You can do this either when adding

list.add(new ArrayList&lt;&gt;(innerlist));

or you reassign innerlist to a new list after adding

list.add(innerlist);
innerlist = new ArrayList&lt;&gt;();

especially if you need to clear it anyway at that point.
See also this question, which handles the same problem, but without an outer list.

huangapple
  • 本文由 发表于 2020年10月14日 11:15:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/64346105.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定