【问题标题】:How to clear one list with values ​from another如何从另一个列表中清除一个列表
【发布时间】:2021-09-09 22:02:49
【问题描述】:

不要告诉我如何修复这段代码,有两个列表。我需要从第二个中清除一个,但是当执行删除方法时,没有任何作用。

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

如果我这样做了

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

然后一个列表被完全清除。删除后想得到类似[d]的东西

【问题讨论】:

  • 欢迎来到 Stack Overflow。请通过tour 了解 Stack Overflow 的工作原理,并阅读How to Ask 以了解如何提高问题的质量。然后edit您的问题将您的源代码包含为minimal reproducible example,可以由其他人编译和测试。请看:What Do You Mean “It Doesn't Work”?
  • removeAll 应该可以工作,前提是 equals 和 hashcode 方法适用于列表的元素,但是由于您将 String 数组放入列表中,因此它可能无法工作。考虑使用 List> 而不是 List
  • 我不明白您的问题,但我可以尝试向您解释您的代码:您有 2 个列表,每个列表都有一个对象。当您调用l.removeAll(l1) 时,不会发生任何事情,因为l1 不包含l 中的任何对象。同样,l.retainAll(l1) 删除了l 中的所有对象,因为ll1 之间没有共同的对象。如果您的预期行为不同,请说明您希望程序做什么。
  • 数组的内容与您的代码 sn-p 无关。您创建了 2 个数组,一个来自 s,一个来自 s1。然后将前者添加到l,将后者添加到l1。事实仍然是它们是两个独立的、不同的数组。它们甚至可以以相同的确切顺序具有相同的确切字符串,并且它们仍然是不同的数组。看起来您只是对这些数组感到困惑。您愿意直接将字符串添加到列表中吗?然后,您的方法将像您在编辑中描述的那样工作。
  • 您需要了解包含一系列字符串的列表与包含一个元素的列表(字符串数组)之间的区别。你有后者。

标签: java java-6


【解决方案1】:

您似乎对将数组添加到列表感到困惑。这有效地为您提供了另一个“层”:您已经获得了列表(ll1),其中包含一个数组,其中包含一些字符串。问题是您将列表视为包含字符串,而实际上它们不包含。让我评论你的代码以显示它是在哪里引入的:

// 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));

我们可以通过消除这个“中间层”来消除很多困惑。以下是如何将字符串直接添加到列表的示例:

// 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]

当您将元素直接添加到如上所示的列表中时,您会得到问题中描述的行为。

【讨论】:

    猜你喜欢
    • 2015-05-17
    • 1970-01-01
    • 2016-09-08
    • 1970-01-01
    • 2015-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多