• Post Reply Bookmark Topic Watch Topic
  • New Topic

repeated elements in ArrayList, cannot figure out why  RSS feed

 
Mona Alsh
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,
I've been trying for soo long to add list to another list and add the result to the a main list ,but I had the same problem of repeated elements whenever I do that!!
Can you please explain to me why do I have this !!!
for example, let say I have List1 = [[3], [2, 1]]
and List2 =[[8], [9, 10]]

I'm looping through the List1 to combine each element in List2.so the output
should be something like this:

[[3,8],[3,9,10],[2,1,8],[2,1,9,10]]

This is what I'm trying to get by using addAll() for subs and add for mainList .

I don't know why do I get some elements repeated, Can you please explain to me why do I have this problem:

[[3, 8, 9, 10], [3, 8, 9, 10], [2, 1, 8, 9, 10], [2, 1, 8, 9, 10]]

Here is my code:



Here is the output:

i=0, j=0
List1=[[3], [2, 1]], subx=[3]
List2=[[8], [9, 10]], suby=[8]
subx after addAll=[3, 8]
mainList=[]
mainList after add=[[3, 8]]
i=0, j=1
List1=[[3, 8], [2, 1]], subx=[3, 8] //why List1 contain [3,8] ,and not [3] I added [3,8] to mainList ?? I didn't change List1, I just use index to refer assign its elements to subx!!
List2=[[8], [9, 10]], suby=[9, 10]
subx after addAll=[3, 8, 9, 10]
mainList=[[3, 8, 9, 10]] //why does mainList contain this before I even I add it , isn't supposed to contain only [[3,8]]
mainList after add=[[3, 8, 9, 10], [3, 8, 9, 10]] //shouldn't suppose to contain only [[3,8],[3,8,9,10]] only!!

I hope I made my point clear this time!!
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mona Alsh wrote:I don't know why do I get some elements repeated

Probably because you have too much code, so the first thing to do is to StopCoding (←click).

What is the basic problem here?

I'd say it's that you have two Lists that you want to combine and ensure that there are no duplicates. The rest is all eyewash (and probably a distraction).

Why don't you have a look at the definition of a Set (java.util.Set) and see how that might help you?

Winston

PS: Please DontWriteLongLines. I've broken yours up this time, but it makes your thread very hard to read.
 
Abdulla S Mamuwala
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A sysout after line no. 63, might answer your question. When i did ,


I got something like this,
Left list [[5], [5, 3], [5, 3, 1]] size: 3
Right List [[8], [8, 7]] size: 2
subL=[5] subR=[8]
[5, 8]
subL=[5, 8] subR=[8, 7] ---> combining [5,8] with [8,7] = [5,8,8,7] ??
[5, 8, 8, 7]
subL=[5, 3] subR=[8]
[5, 3, 8]
subL=[5, 3, 8] subR=[8, 7]
[5, 3, 8, 8, 7]
subL=[5, 3, 1] subR=[8]
[5, 3, 1, 8]
subL=[5, 3, 1, 8] subR=[8, 7]
[5, 3, 1, 8, 8, 7]
[[5, 8, 8, 7], [5, 8, 8, 7], [5, 3, 8, 8, 7], [5, 3, 8, 8, 7], [5, 3, 1, 8, 8, 7], [5, 3, 1, 8, 8, 7]]
 
Mona Alsh
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
Mona Alsh wrote:I don't know why do I get some elements repeated

Probably because you have too much code, so the first thing to do is to StopCoding (←click).

What is the basic problem here?

I'd say it's that you have two Lists that you want to combine and ensure that there are no duplicates. The rest is all eyewash (and probably a distraction).

Why don't you have a look at the definition of a Set (java.util.Set) and see how that might help you?




yes, sorry for posting my whole code for the problem, I modified the post to display only my doubt.
I know what the set is used for but I don't need it!
the problem is not an issue, the issue is that I want to have lists that contain every element from List1 combined with each element in List separately. and the thing I get is it combined and repeated.
and when I print the final mainList , I get all of the elements combined!!!

Thanks Winston
 
Mona Alsh
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abdulla S Mamuwala wrote:A sysout after line no. 63, might answer your question. When i did ,


I got something like this,
Left list [[5], [5, 3], [5, 3, 1]] size: 3
Right List [[8], [8, 7]] size: 2
subL=[5] subR=[8]
[5, 8]
subL=[5, 8] subR=[8, 7] ---> combining [5,8] with [8,7] = [5,8,8,7] ?? why is subL = [5,8] it is still subL = leftList.get(0) which is subL = [5]
[5, 8, 8, 7]
subL=[5, 3] subR=[8]
[5, 3, 8]
subL=[5, 3, 8] subR=[8, 7]
[5, 3, 8, 8, 7]
subL=[5, 3, 1] subR=[8]
[5, 3, 1, 8]
subL=[5, 3, 1, 8] subR=[8, 7]
[5, 3, 1, 8, 8, 7]
[[5, 8, 8, 7], [5, 8, 8, 7], [5, 3, 8, 8, 7], [5, 3, 8, 8, 7], [5, 3, 1, 8, 8, 7], [5, 3, 1, 8, 8, 7]]


Abdullah, yes that is my question , every time in the loop , I assign to the original values from leftList and rightList every time
subL = leftList.get(i);
subR =leftList.get(j);
why there value are updated since I'm assigning to the original values from the leftList and rightList in every iteration

Thank you very much!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Add more print statements. Post your new code, with the print statements in it. Indicate exactly what you got and what you expected instead at the most detailed level where you see a discrepancy.

Right now, trying to understand what you're trying to do and what you're seeing just from descriptions is not useful.

For example:
 
Abdulla S Mamuwala
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mona, looks like on line 33 and 34 you are referencing subx and suby to List1 and List2 respectively.



Then on line 35, you are changing the contents of your List1


I did a sysout and was seeing how List1 contents are being altered,

[[3], [2, 1]]
[[8], [9, 10]]
i=0 j=0 subx=[3] suby=[8] mainList=[] List1.get(i)=[3]
i=0 j=0 subx=[3, 8] suby=[8] mainList=[[3, 8]] List1.get(i)=[3, 8]
-------------*-------------------------------------
[[3, 8]]
i=0 j=1 subx=[3, 8] suby=[9, 10] mainList=[[3, 8]] List1.get(i)=[3, 8]
i=0 j=1 subx=[3, 8, 9, 10] suby=[9, 10] mainList=[[3, 8, 9, 10], [3, 8, 9, 10]] List1.get(i)=[3, 8, 9, 10]

-------------*-------------------------------------

I believe contents of List1 should not be changing, in order to get the result you are looking to achieve!

You could clone the contents of List1 into subx instead of doing "subx = List1.get(i)" which I believe is giving subx a reference to List1 and making changes directly to List1 when you do

 
Mona Alsh
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abdulla S Mamuwala wrote:

I believe contents of List1 should not be changing, in order to get the result you are looking to achieve!

You could clone the contents of List1 into subx instead of doing "subx = List1.get(i)" which I believe is giving subx a reference to List1 and making changes directly to List1 when you do



Thank you Abdullah soo much,, I figured out the problem as you said,,the assignment to subx and then the addAll changed the elements of the List1.
I honestly didn't know that before I though these are two different variables and they couldn't change the elements of the List1.

so how I do the clone of the List1!!!
I really appreciate your help!!

Thanks again
 
Abdulla S Mamuwala
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mona, I think you are pretty close to the solution. You, now need to do a trail and error approach to get your result, keeping in mind how Java references work. Maybe you don't even require to clone your lists.

1. create new instances of subx in the loop that runs j.
2. add List1 and List2 to subx.
3. Add subx to mainList.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!