【问题标题】:java : remove words from ArrayList<String>java : 从 ArrayList<String> 中删除单词
【发布时间】:2017-06-13 22:25:50
【问题描述】:

让我们说我的话 Array 是 words={"a","the","in","if","are","it","is"} 并且我的 ArrayList 包含这样的字符串 {"a桌子在这里”,“书卖了”,“如果它是可读的”}。我想从数组列表中删除数组的所有单词。 预期输出将是 ArrayList 为 {"table here","books sold","re​​adable"}。

到目前为止我已经尝试过了:

public static void main(String[] args) {
        String[] words = {"a","the","in","if","are","it","is"};
        List<String> wordList = new ArrayList<String>(Arrays.asList(words));
        String[] tArray = {"a table is here", "books are sold","if it is readable"};
        List<String> list = new ArrayList<String>(Arrays.asList(tArray));

        for (int i = 0; i < list.size(); i++) {
            String[] tArrays = list.get(i).split(" ");
            List<String> line = new ArrayList<String>(Arrays.asList(tArrays));
                for (int c = 0; c < wordList.size(); c++) {
                    if (wordList.get(c).equals(line.get(i))) {
                        line.remove(i);
                        i--;
                        break;
                    }
                }//end for
            list.set(i, String.join(" ", line));
        }//end for
        for(String string : list) 
        {
            System.out.println(string);
        }
    }

但没有给出预期的输出。而是给出错误“java.lang.ArrayIndexOutOfBoundsException:-1”

【问题讨论】:

  • 您是否尝试过调试代码并检查发生了哪一行异常以及为什么值变为负数?
  • 计划使用line.size()对基于“ ”拆分后的所有单词进行迭代。
  • @dbf 可能是因为 "i--;"我试过调试它..
  • 这样做: for (int c = 0; c

标签: java arrays arraylist linked-list


【解决方案1】:

您应该使用line.size() 来迭代基于“ ”拆分后的所有单词。

for (int i = 0; i < list.size(); i++) {
        String[] tArrays = list.get(i).split(" ");
        List<String> line = new ArrayList<>(Arrays.asList(tArrays));
        for (int c = 0; c < wordList.size(); c++) {
            if (wordList.get(c).equals(line.get(i))) { // you get i for indexes corresponding to the sentence element and not `line` elements as created above
                line.remove(i); // i is iterated over the list of sentences in your case 3, would start from i=0
                i--; // you change it to '-1' and get IndexOutOfBounds
                break;
            }
        }//end for
        list.set(i, String.join(" ", line));
    }//end for

虽然没有经过测试,但你可以做类似的事情 -

public static void main(String[] args) {
    String[] words = {"a", "the", "in", "if", "are", "it", "is"};
    List<String> wordList = new ArrayList<>(Arrays.asList(words));
    String[] tArray = {"a table is here", "books are sold", "if it is readable"};
    List<String> list = new ArrayList<>(Arrays.asList(tArray));

    for (int i = 0; i < list.size(); i++) {
        String[] tArrays = list.get(i).split(" ");
        List<String> line = new ArrayList<>(Arrays.asList(tArrays));
        for (String lineElement : line) {
            if (wordList.contains(lineElement)) {
                line.remove(lineElement);
            }
        }

        list.set(i, String.join(" ", line));
    }

    for (String string : list) {
        System.out.println(string);
    }
}

【讨论】:

    【解决方案2】:

    只是添加了做这种过滤的java 8版本:

    String[] filtered = 
            list.stream().map(statement -> Arrays.asList(statement.split(" ")))
                         .map(listOfWords -> listOfWords.stream()
                                .filter(word -> !wordList.contains(word))
                                .collect(Collectors.joining(" "))
                             )
                             .toArray(String[]::new);
    List<String> filteredList = Arrays.asList(filtered);
    

    您可以使用以下方法验证输出:

    filteredList.stream().forEach(System.out::println);
    

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-11
      相关资源
      最近更新 更多