将输入拆分两次

我目前遇到了一个列表问题。列表看起来像这样


a b c d

a d

a c

d b

...

第一行是索引,使用后将被删除。其余的将在以后需要。


while ((strLine = br.readLine()) != null)   {

line.add(strLine);

}

//...

line.remove(0);

这样就创建了一个列表(“行”)。现在我想通过删除空格再次拆分列表。


任何帮助,将不胜感激!先感谢您!


德玛西亚99
浏览 179回答 3
3回答

皈依舞

分割每一行并过滤空间。忽略循环中的第一行:boolean firstLine = true;List<List<Character>> chars = new ArrayList<>();while ((strLine = br.readLine()) != null) {&nbsp; &nbsp; if (firstLine) {&nbsp; &nbsp; &nbsp; &nbsp; firstLine = !firstLine;&nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; }&nbsp; &nbsp; chars.add(strLine.chars().filter(e -> e != ' ').mapToObj(e -> (char) e).collect(Collectors.toList()));}字符包含输出:[['a', 'b'], ['c', 'd']]

明月笑刀无情

正如你在评论中提到的,你需要['a', 'b', 'c', 'd', 'a', 'd', ...]&nbsp;这个列表如果输入是这样的:a b c da da cd b...所以你想出了如何做到这一点:["a b c d", "a d", "a c", "d b", ...]&nbsp;我会告诉你接下来的步骤。让这个列表成为lines。现在,遍历 的所有元素,lines然后遍历该元素的每个字符,并将所需信息保存在另一个List.for (String line: lines) {&nbsp; &nbsp; for (char chr: line) {&nbsp; &nbsp; &nbsp; &nbsp; if (chr == ' ') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; characters.add(chr);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}现在您的characters列表将如下所示['a', 'b', 'c', 'd', 'a', 'd', ...]

www说

String[] strArray = new String[letters.size()];&nbsp; &nbsp; strArray = letters.toArray(strArray);&nbsp; &nbsp; letters.clear();&nbsp; &nbsp; for(String A:strArray) {&nbsp; &nbsp; &nbsp; &nbsp; String[] arrOfStr = A.split(" ");&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; for(String B:arrOfStr) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; letters.add(B);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }输出将如下所示[a, b, c, d, a, d, a, c, d, b]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java