迭代 arraylist 直到找到特定字符并停止迭代
Iterating arraylist until find an specific character and stop the iteration
我正在尝试迭代数组列表,然后在找到字符(在本例中为逗号)时停止。这是我的:
这是数组列表:
List<String> collection = Arrays.asList(new String[]{"I", "f", " ", "y", "o", "u", " ", "g", "e", "t", " ", "a", " ", "t", "e", "x", "t", " ", "m", "e", "s", "s", "a", "g", "e", " ", "f", "r", "o", "m", " ", "a", "n", " ", "e", "m", "a", "i", "l", " ", "a", "d", "d", "r", "e", "s", "s", " ", "o", "r", " ", "n", "u", "m", "b", "e", "r", " ", "y", "o", "u", " ", "d", "o", "n", "'", "t", " ", "r", "e", "c", "o", "g", "n", "i", "z", "e", ",", " ", "i", "t", "'", "s", " ", "p", "r", "o", "b", "a", "b", "l", "y", " ", "b", "e", "s", "t", " ", "t", "o", " ", "i", "g", "n", "o", "r", "e", " ", "i", "t"});
这是我在 main 上使用的方法:showUntil();
private static void showUntil() {
for(String g : collection){
if(g.equals(",")){
System.out.println(g);
}
}
}
我正在使用 java 进行此练习。
您可以使用 indexOf
函数并迭代到该索引。这是一种方法
List<String> collection = Arrays.asList(new String[]{"I", "f", " ", "y", "o", "u", " ", "g", "e", "t", " ", "a", " ", "t", "e", "x", "t", " ", "m", "e", "s", "s", "a", "g", "e", " ", "f", "r", "o", "m", " ", "a", "n", " ", "e", "m", "a", "i", "l", " ", "a", "d", "d", "r", "e", "s", "s", " ", "o", "r", " ", "n", "u", "m", "b", "e", "r", " ", "y", "o", "u", " ", "d", "o", "n", "'", "t", " ", "r", "e", "c", "o", "g", "n", "i", "z", "e", ",", " ", "i", "t", "'", "s", " ", "p", "r", "o", "b", "a", "b", "l", "y", " ", "b", "e", "s", "t", " ", "t", "o", " ", "i", "g", "n", "o", "r", "e", " ", "i", "t"});
for(String chr : collection.subList(0, collection.indexOf(","))) {
System.out.println(chr);
}
如果你想更正你写的代码,如果你找到像下面这样的匹配,你需要跳出循环
for(String g : collection) {
if(g.equals(",")) {
System.out.println(g);
break; //exit the loop if comma is found!
}
}
我正在尝试迭代数组列表,然后在找到字符(在本例中为逗号)时停止。这是我的:
这是数组列表:
List<String> collection = Arrays.asList(new String[]{"I", "f", " ", "y", "o", "u", " ", "g", "e", "t", " ", "a", " ", "t", "e", "x", "t", " ", "m", "e", "s", "s", "a", "g", "e", " ", "f", "r", "o", "m", " ", "a", "n", " ", "e", "m", "a", "i", "l", " ", "a", "d", "d", "r", "e", "s", "s", " ", "o", "r", " ", "n", "u", "m", "b", "e", "r", " ", "y", "o", "u", " ", "d", "o", "n", "'", "t", " ", "r", "e", "c", "o", "g", "n", "i", "z", "e", ",", " ", "i", "t", "'", "s", " ", "p", "r", "o", "b", "a", "b", "l", "y", " ", "b", "e", "s", "t", " ", "t", "o", " ", "i", "g", "n", "o", "r", "e", " ", "i", "t"});
这是我在 main 上使用的方法:showUntil();
private static void showUntil() {
for(String g : collection){
if(g.equals(",")){
System.out.println(g);
}
}
}
我正在使用 java 进行此练习。
您可以使用 indexOf
函数并迭代到该索引。这是一种方法
List<String> collection = Arrays.asList(new String[]{"I", "f", " ", "y", "o", "u", " ", "g", "e", "t", " ", "a", " ", "t", "e", "x", "t", " ", "m", "e", "s", "s", "a", "g", "e", " ", "f", "r", "o", "m", " ", "a", "n", " ", "e", "m", "a", "i", "l", " ", "a", "d", "d", "r", "e", "s", "s", " ", "o", "r", " ", "n", "u", "m", "b", "e", "r", " ", "y", "o", "u", " ", "d", "o", "n", "'", "t", " ", "r", "e", "c", "o", "g", "n", "i", "z", "e", ",", " ", "i", "t", "'", "s", " ", "p", "r", "o", "b", "a", "b", "l", "y", " ", "b", "e", "s", "t", " ", "t", "o", " ", "i", "g", "n", "o", "r", "e", " ", "i", "t"});
for(String chr : collection.subList(0, collection.indexOf(","))) {
System.out.println(chr);
}
如果你想更正你写的代码,如果你找到像下面这样的匹配,你需要跳出循环
for(String g : collection) {
if(g.equals(",")) {
System.out.println(g);
break; //exit the loop if comma is found!
}
}