2017-11-25 20:55:26
0
I'm new to Java and I'm trying (and - apparently- failing) to combine two string sets if they have common elements:
CommonElements = Set1;
CommonElements.retainAll(Set2);
System.out.println("common elements of"+Set1+"and"+Set2+":"+CommonElements);
if (CommonElements.size()!=0) {
Set1.addAll(Set2);
System.out.println("both sets"+Set1);
}
Now, I am applying this same thing though I now have an ArrayList of sets:
for (i=0; i
for (int j = 1; j
Set Set1 = myList.get(i);
System.out.println("Set1"+Set1);
Set Set2 = myList.get(i+j);
System.out.println("Set2"+Set2);
Set commonElements = myList.get(i);
commonElements.retainAll(Set2);
System.out.println("Common elements of"+i+"and"+(i+j)+":"+Set3);
if (commonElements.size()!=0) {
Set1.addAll(Set2);
myList.set(i, Set1);
System.out.println("both sets"+Set1);
}
System.out.println(myList.get(i));
But System.out.println(myList.get(i)) results in:
if the two sets don't have anything in common:
Set1 becomes empty, myList.get(i) returns [].
if the two sets do have something in common:
Set1 becomes Set2, so List entry number i takes the value of List entry number i+j.