[英]Add Item to the ArrayList of ArrayList
I have an ArrayList of ArrayList like the following code and I'm intended to add one item to an existed arraylist and save it as well as keeping the old arrayList. 我有一个类似于下面代码的ArrayList的ArrayList,我打算将一项添加到现有的arraylist中并保存它,同时保留旧的arrayList。
ArrayList<ArrayList<Integer>> bigArr = new ArrayList<ArrayList<Integer>> ();
ArrayList<Integer> arr = new ArrayList<Integer> ();
arr.add(1);
arr.add(2);
arr.add(3);
bigArr.add(arr);
ArrayList<Integer> tt = bigArr.get(0);
tt.add(4);
bigArr.add(tt);
System.out.println(bigArr);
But the thing is that happens is that it prints [[1, 2, 3, 4], [1, 2, 3, 4]] instead of [[1, 2, 3], [1, 2, 3, 4]]. 但是发生的事情是它打印了[[1、2、3、4],[1、2、3、4]],而不是[[1、2、3],[1、2、3、4] ]]。 Can someone please tell me what should I do to get the second output?
有人可以告诉我我应该怎么做才能获得第二个输出吗?
Create two lists instead of reusing the first list. 创建两个列表,而不是重用第一个列表。
ArrayList<ArrayList<Integer>> bigArr = new ArrayList<ArrayList<Integer>> ();
ArrayList<Integer> arr = new ArrayList<Integer> ();
arr.add(1);
arr.add(2);
arr.add(3);
bigArr.add(arr);
ArrayList<Integer> tt = new ArrayList<Integer>(arr); // Create a new list based on the elements of the given list
tt.add(4);
bigArr.add(tt);
System.out.println(bigArr);
Object
type parameter's references are pass by value but the memory location that is pointed by them remains same.Object
类型参数的引用按值传递,但它们指向的内存位置保持不变。 So changing anything using any reference variable will affect the sameObject
.因此,使用任何引用变量更改任何内容都会影响同一
Object
。
So here tt
and arr
is pointing to the same memory location means if you change something in one of them that gets reflected to other as well. 因此,此处的
tt
和arr
指向相同的内存位置,这意味着如果您更改其中之一,并且也会反映给其他人。
Because your tt
is still pointing to the same arraylist object. 因为您的
tt
仍指向相同的arraylist对象。 It's modifying the same object. 它正在修改同一个对象。
Solution: Make a new list object by copying old list and add item in that list then add list to main list. 解决方案: 通过复制旧列表并在该列表中添加项目,然后将列表添加到主列表来创建新列表对象。
List
s are objects with mutable state. List
是具有可变状态的对象。 This means that adding or removing items will change the state of the List
your variable is pointing to. 这意味着添加或删除项目将更改变量所指向的
List
的状态。 You create a single arr = ArrayList<Integer>
, to which you add the values 1, 2, 3
. 您创建一个单一的
arr = ArrayList<Integer>
,并在其中添加值1, 2, 3
。 You then add this list to the bigArr
list of lists. 然后,您将此列表添加到列表的
bigArr
列表中。 Now, the first element of bigArr
and arr
both point to the same physical object. 现在,
bigArr
和arr
的第一个元素都指向同一个物理对象。 The moment you retrieve it from bigArr
and store it in the tt
variable, you have three ways to access the same list. 从
bigArr
检索它并将其存储在tt
变量中之后,便可以通过三种方式访问同一列表。
What you want, is a new version (copy) of your initial list at bigArr.get(0)
and add the new value to that list. 您想要的是在
bigArr.get(0)
处的初始列表的新版本(副本),并将新值添加到该列表中。 The easiest way to do this is to use ArrayList
's copy constructor when you retrieve it from the bigArr
. 最简单的方法是从
bigArr
检索ArrayList
的副本构造bigArr
。 This is already explained in the answer given by Smutje: Smutje给出的答案已经对此进行了解释:
ArrayList<Integer> tt = new ArrayList<Integer> (arr);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.