简体   繁体   English

将项添加到ArrayList的LinkedList大小错误

[英]Adding item to LinkedList of ArrayLists size error

Okey, in my program I created LinkedList of ArrayLists: Okey,在我的程序中我创建了ArrayLists的LinkedList:

 List<Marker> list1 = new ArrayList<Marker>();
 LinkedList<List<Marker>> list2 = new LinkedList<List<Marker>>();

Everything works fine while adding items to list1: list1.add(mark); 将项目添加到list1时,一切正常: list1.add(mark);

But when I try to add my list1 to list2 ( list2.add(list1) ), I am getting strange things: each list size in list2 becomes equals to last added list size+1, and i can't even read these lists items with (for example) list2.getFirst().get(0); 但是当我尝试将list1添加到list2( list2.add(list1) )时,我发现了一些奇怪的事情:list2中的每个列表大小都等于最后添加的列表大小+ 1,我甚至无法读取这些列表项with(例如) list2.getFirst().get(0); - doing this I got error; - 这样做我得到了错误;

For example I am doing this: 例如我这样做:

mark.addSomeInfo();
list1.add(mark);
list2.add(list1);
mark.addSomeInfo();
list1.add(mark);
list2.add(list1);
mark.addSomeInfo();
list1.add(mark);
list2.add(list1);

And I need to get this: 我需要得到这个:

list2.get(0).size() = 1;
list2.get(1).size() = 2;
list2.get(2).size() = 3;

But instead I am getting this: 但相反,我得到了这个:

list2.get(0).size() = 4;
list2.get(1).size() = 4;
list2.get(2).size() = 4;

Why? 为什么? What I am doing wrong? 我做错了什么?

The LinkedList<List<Marker>> list2 add method does not create a copy of the list you added into it. LinkedList<List<Marker>> list2 add方法不会创建您添加到其中的列表的副本。 It only adds a reference of list1 to list2 . 它只将list1的引用添加到list2 So in essence your list2 holds 3 references to the same list. 所以实质上你的list2拥有3个对同一个列表的引用。

What you want to do is create the copies yourself as you are placing them into list2 您要做的是在将它们放入list2自己创建副本

mark.addSomeInfo();
list1.add(mark);
list2.add(new ArrayList<Mark>(list1));
mark.addSomeInfo();
list1.add(mark);
list2.add(new ArrayList<Mark>(list1));
mark.addSomeInfo();
list1.add(mark);
list2.add(new ArrayList<Mark>(list1));

All the reference of LinkedList refers to same List Object which is list1 so it prints size 4. LinkedList所有引用都引用相同的List对象,即list1因此它打印大小为4。

As you used ArrayList which supports duplicate element so every time you add mark object it will make a duplicate entry. 当您使用支持重复元素的ArrayList ,每次添加标记对象时,它都会生成重复条目。

List2中的引用是针对List1的。这就是为什么所有相​​同的SOP给出相同的结果4

list1 is just a reference in your code.So in essence, you are adding the same arraylist object to the linkedlist again and again. list1只是你代码中的一个引用。实质上,你是一次又一次地将相同的arraylist对象添加到linkedlist And in the end the same arraylist is copied 3 times in your example and has size 4. 最后,在您的示例中,相同的arraylist被复制3次,大小为4。

At 0th,1st and 2nd position of list2, you have added same list1 object. 在list2的第0个,第1个和第2个位置,添加了相同的list1对象。 which is being changed by 正在被改变

 list1.add(mark);

As the above function has been called four times in your code.Hence list1 reference now points to the object on heap which has 4 items, and hence size of all 3 items of list1 has 4 elements. 由于上面的函数已经在你的代码中调用了四次.Hence list1引用现在指向堆上有4个项目的对象,因此list1的所有3个项目的大小都有4个元素。

mark.addSomeInfo();
list1.add(mark);  //this gets added to list each time you add it --1
list2.add(list1); //this gets added too as many times as you add list1
mark.addSomeInfo();
list1.add(mark);  // 2
list2.add(list1);  //2
mark.addSomeInfo();
list1.add(mark);  // 3
list2.add(list1);  // 3

So after this you will have 3 elements in list1 and 3 lists in list2 因此,在此之后,list1中将包含3个元素,list2中将包含3个列表

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

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