-8

Do not tell me how I can fix this code, there are two lists.I need to clear one from the second, but when the delete method is executed, nothing works.

List<String[]> l = new ArrayList<>();
List<String[]> l1 = new ArrayList<>();
final String s = "a,b,c";
final String s1 = "a,b,c,d";

l.add(s.split(",", -1));
l1.add(s1.split(",", -1));

System.out.println(Arrays.asList(l.get(0)));        //[a, b, c]
System.out.println(Arrays.asList(l1.get(0)));       //[a, b, c, d]

System.out.println(l.removeAll(l1));                //false

and if I do

 System.out.println(l.retainAll(l1));                //true

then one list is completely cleared. After deleting, I want to get something similar to [d]

  • 3
    Welcome to Stack Overflow. Please take the [tour] to learn how Stack Overflow works and read [ask] on how to improve the quality of your question. Then [edit] your question to include your source code as a [mcve], which can be compiled and tested by others. Please see: [What Do You Mean “It Doesn't Work”?](https://meta.stackexchange.com/q/147616) – Progman Sep 09 '21 at 22:05
  • removeAll should work provided that equals and hashcode methods work properly for elements of the list, but since you put array of String into a list it's probably not going to work. Consider using List> instead of List. – mvmn Sep 09 '21 at 22:09
  • 1
    I don't understand your question, but I can try to explain your code back to you: You have 2 lists, each have one object. When you call `l.removeAll(l1)`, nothing should happen because `l1` doesn't contain any objects that are also in `l`. Similarly, `l.retainAll(l1)` removes all the objects in `l` because there are no objects in common between `l` and `l1`. If your expected behavior is different, please explain what you expect the program to do. – Charlie Armstrong Sep 09 '21 at 22:18
  • Do you mean for the `List`s to be `List`s? – John Kugelman Sep 09 '21 at 22:21
  • @CharlieArmstrong But they differ by 1 character. How can I point out that they are similar? – Lexxington Sep 09 '21 at 22:38
  • 1
    The content of the arrays is irrelevant in your code snippet. You created 2 arrays, one from `s` and one from `s1`. You then added the former to `l` and the latter to `l1`. The fact still remains that they are two separate, distinct arrays. They could even have the same exact strings in the same exact order, and they would still be different arrays. It looks like you're just confusing yourself with these arrays. Would you be willing to just add the strings directly to the lists? Then your approach will work like you've described in your edit. – Charlie Armstrong Sep 09 '21 at 22:51
  • @CharlieArmstrong I want the program to remove values ​​from the first list in the second – Lexxington Sep 09 '21 at 22:53
  • 2
    You need to understand the difference between a List which contains a series of Strings, and a List with one element which is an array of Strings. You have the latter. – tgdavies Sep 09 '21 at 22:55
  • @Lexxington You might want to check https://stackoverflow.com/questions/8777257/equals-vs-arrays-equals-in-java on what is happening with `equals()` on arrays. – Progman Sep 10 '21 at 17:03

1 Answers1

0

It looks like you've confused yourself by adding arrays to lists. This effectively gives you another "layer": You've got the list (l or l1), which contains an array, which contains some strings. The problem is you're thinking of the lists as if they contained strings, when they do not. Let me comment your code to show where this got introduced:

// String[] is an array of strings
// List<String> is a list of strings
// List<String[]> is a list of arrays of strings
List<String[]> l = new ArrayList<>();
List<String[]> l1 = new ArrayList<>();

final String s = "a,b,c";
final String s1 = "a,b,c,d";

// s.split(",", -1) returns an array of strings
// You then add this array to your list of arrays
l.add(s.split(",", -1));
l1.add(s1.split(",", -1));

// This shows the strings because of the Arrays.asList() call
// l.get(0) returns the array from the list, then Arrays.asList()
// converts that array to a new list, which prints out nice and pretty
System.out.println(Arrays.asList(l.get(0)));
System.out.println(Arrays.asList(l1.get(0)));

// Here, you are trying to remove the array stored in l1 from l
// l doesn't contain the array stored in l1, it has a different array
// So nothing happens
System.out.println(l.removeAll(l1));

We can clear up a lot of confusion by eliminating this "middle layer." Below is an example of how you could add the strings directly to the list:

// Type is List<String>
List<String> l = new ArrayList<>();
List<String> l1 = new ArrayList<>();

final String s = "a,b,c";
final String s1 = "a,b,c,d";

// We need to convert the arrays to lists using Arrays.asList()
// prior to calling addAll()
l.addAll(Arrays.asList(s.split(",", -1)));
l1.addAll(Arrays.asList(s1.split(",", -1)));

// We can print the lists out directly here, rather than
// messing with converting arrays
System.out.println(l);                       // [a, b, c]
System.out.println(l1);                      // [a, b, c, d]

System.out.println(l1.removeAll(l));         // true
System.out.println(l1);                      // [d]

When you add the elements directly to the list as shown above, you get the behavior you describe in the question.

Charlie Armstrong
  • 2,332
  • 3
  • 13
  • 25