【问题标题】:Java LinkedList removing zero length membersJava LinkedList 删除零长度成员
【发布时间】:2014-09-22 09:32:02
【问题描述】:

我正在尝试从长度为零的 LinkedList 中删除成员。我试图使代码动态化,它从字符串中删除所有空格,但将单词分开并按顺序排列。我尝试通过将字符串转换为数组并在空格处拆分它来实现这一点。然后我根据自己的喜好将其转换为 LinkedList。

private void sliceItUpFreashness() {
        String s = "    hello there"; // four spaces at beginning of string
        String[] sa = s.split(" ");
        LinkedList<String> ll = new LinkedList<>(Arrays.asList(sa));

        for (int i = 0; i < ll.size(); i++) {
            System.out.println("Size for ll("+i+") == "+ll.get(i).length()); // confirms length of the members is 0
            if (ll.get(i).length() == 0) {
                ll.remove(i);
            }
        }
        System.out.println("-----");
        for (String a : ll) { // this confirms if the zero-length members were removed
            System.out.println(a);
        }
    }

但是,上面的代码不会删除长度为 0 或 null 的成员。我能做什么?

【问题讨论】:

  • 不要在循环中删除。请改用iterator

标签: java arrays string linked-list


【解决方案1】:

我建议您迭代一次数组并直接构建LinkedList -

String s = "    hello there"; // four spaces at beginning of string
String[] sa = s.split(" ");
List<String> ll = new LinkedList<>();
for (String str : sa) {
  if (str.length() > 0) { // <-- check for empty string.
    ll.add(str);
  }
}

另一种解决方案是迭代字符串本身并跳过split

List<String> ll = new LinkedList<>();
StringBuilder sb = new StringBuilder();
for (char ch : s.toCharArray()) { // <-- String toCharArray
  if (ch != ' ') {
    sb.append(ch); // <-- not a space.
  } else {
    if (sb.length() > 0) { // <-- add non zero length strings to List
      ll.add(sb.toString());
      sb.setLength(0);
    }
  }
}
// Add the last of the buffer
if (sb.length() > 0) {
  ll.add(sb.toString());
}

【讨论】:

  • 完美运行,谢谢。如果我可以投票,我会的。延迟结束后,我会尽快投票:)
  • @user3892311 你可以接受他的回答
【解决方案2】:

或者使用Collections.singleton("")

    String s = "    hello there"; // four spaces at beginning of string
    String[] sa = s.split(" ");
    List<String> list = new ArrayList<String>(Arrays.asList(sa));
    list.removeAll(Collections.singleton(""));

【讨论】:

  • 嗨,我不熟悉 Collections.singleton 的作用。你能给我解释一下吗?
  • Collections.singleton 返回一个只包含指定对象的不可变集合。所以我制作了一个包含空字符串的集合,然后使用 remove all 从父列表中删除。
  • 简而言之,一行类似于写这三行:Set&lt;String&gt; set = new HashSet&lt;String&gt;(); set.add(""); list.removeAll(set);
【解决方案3】:

代码:

String s = "    hello there"; // four spaces at beginning of string
    String[] sa = s.split(" ");
    LinkedList<String> ll = new LinkedList<>(Arrays.asList(sa));

    ll.stream()
      .filter( s1 -> !s1.isEmpty())
      .map(s1-> s1)
      .forEach(System.out::print);

输出:

    hello there
hellothere

希望对您有所帮助,如果您需要更多解释,请告诉我

注意:我使用 Java 8 解决了这个问题。

【讨论】:

  • 嗨,我在 java 7 上运行,所以我不明白你发布的最后四行代码。你能给我解释一下吗:)?
  • 这是 java 8 中引入的 lamda 表达式。另外需要注意的是Stream - “支持顺序和并行聚合操作的元素序列。”再次在 Java 8 中引入。该代码的作用是遍历列表并过滤掉空字符串,然后使用映射获取结果流,然后再次使用 forEach 打印出每个值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-01-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多