【问题标题】:Unable to remove empty elements from ArrayList of String [duplicate]无法从字符串的 ArrayList 中删除空元素 [重复]
【发布时间】:2017-09-12 13:48:56
【问题描述】:

我面临与In Java, remove empty elements from a list of Strings 相同的情况。 我尝试了几乎所有我能得到的资源,但每次我得到同样的错误"Exception in thread "main" java.lang.UnsupportedOperationException"

public static void main(String[] args) {
    String s = "Hello World. Want to code?";
    StringTokenizer tokenizer = new StringTokenizer(s, ".!?");

    List<String> words = new ArrayList<String>();
    List<String> statement = new ArrayList<String>();
    List<List<String>> statements = new ArrayList<List<String>>();

    // Seperating words by delimiters ".!?
    while (tokenizer.hasMoreTokens()) {
        words.add(tokenizer.nextToken());
    }
    // O/p is {{Hello World},{ Want to code}}

    // seperating words by space.
    for (int i = 0; i < words.size(); i++) {
        String[] temp2 = words.get(i).split("\\s+");
        statement = Arrays.asList(temp2);
        statements.add(statement);
    }
    // O/P is {{Hello, World},{, Want, to, code}}

    for (List<String> temp : statements) {
    // Here i have [, Want, to, code]   

        // Way-1
        Iterator it = temp.iterator();
        String str = (String) it.next();
        if(str.isEmpty())
            it.remove();

        // Way-2
        temp.removeIf(item -> item.contains(""));

        // Way-3
        temp.removeAll(Collections.singleton(""));

        // Way-4
        temp.removeAll(Arrays.asList(""));

        // way-5
        temp.removeIf(String::isEmpty);
    }
}

如您所见,我尝试了 4 种方法,但都没有奏效。 有人知道吗?

【问题讨论】:

  • 试试temp.removeIf(String::isEmpty)
  • 不工作。 :( 我编辑了问题。
  • 对不起,我的意思是作为您其他 4 种方式的更好替代方案,而不是作为您问题的答案。答案在重复链接中。
  • 在哪里你有new ArrayList&lt;String&gt;(Arrays.asList(stringArray))??我在代码中只看到两个asList() 调用,并且它们都没有 被转换为ArrayList。请注意,list = new ArrayList(); list.add(Arrays.asList(...));list = new ArrayList(Arrays.asList(...)); 相同。也许你应该 ArrayList(otherList) 构造函数的 read the javadoc,看看它做了什么。
  • 是的,我在一分钟内删除了那个问题。我很抱歉,也谢谢。它的工作。 :)

标签: java string list arraylist


【解决方案1】:

我想我找到了解决办法:将List的数据类型改为ArrayList。这为我解决了这个问题:

import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.util.Collections;

public class Test {
    public static void main(String[] args) {
        String s = "Hello World. Want to code?";
        StringTokenizer tokenizer = new StringTokenizer(s, ".!?");

        ArrayList<String> words = new ArrayList<String>();
        ArrayList<String> statement = new ArrayList<String>();
        ArrayList<ArrayList<String>> statements = new ArrayList<ArrayList<String>>();

        // Seperating words by delimiters ".!?
        while (tokenizer.hasMoreTokens()) {
            words.add(tokenizer.nextToken());
        }
        // O/p is {{Hello World},{ Want to code}}

        // seperating words by space.
        for (int i = 0; i < words.size(); i++) {
            String[] temp2 = words.get(i).split("\\s+");
            statement = new ArrayList();
            statement.addAll(Arrays.asList(temp2));
            statements.add(statement);
        }
        // O/P is {{Hello, World},{, Want, to, code}}

        for (ArrayList<String> temp : statements) {
        // Here i have [, Want, to, code]

            // Way-2
            temp.removeIf(item -> item.equals(""));
            System.out.println("array: " + temp);
        }
    }
}

程序的输出是这样的:

array: [Hello, World]
array: [Want, to, code]

【讨论】:

  • 问题是关于UnsupportedOperationException
  • @Andreas 是的,谢谢,我已经确定了答案
猜你喜欢
  • 2017-09-25
  • 2023-01-26
  • 2016-07-09
  • 2012-07-10
  • 2021-08-30
  • 1970-01-01
  • 2023-04-09
  • 2016-01-19
  • 1970-01-01
相关资源
最近更新 更多