Arraylist 匹配并返回一个布尔结果
我在将字符串与字符串数组列表匹配并获取单个布尔结果时遇到问题。基本上,我使用 for 循环来进行匹配,我得到的只是一系列布尔值。但我想要的是,当所有布尔值中有一个时,它将返回一个值,如果是 all ,那么它将返回一个值。代码如下。帮助TT
import java.util.*;
public class NewClass {
public static void main(String [] args){
ArrayList <String> aList = new ArrayList <String>();
aList.add("I");
aList.add("Love");
aList.add("You");
aList.add("Black");
aList.add("Colored");
aList.add("Ferrari");
boolean match;
for(int i = 0; i < aList.size();i++){
match = aList.get(i).equals("Red");
System.out.print(match);
}
}
}
I have a problem with matching a string against a string arraylist and getting a single boolean result. Basically, I used a for loop to do the matching and all I get was a series of boolean. But what I want is that when there is one amongst all the boolean, it will return one single value and if it is all , then it will return a single value. The code is as below. Help T.T
import java.util.*;
public class NewClass {
public static void main(String [] args){
ArrayList <String> aList = new ArrayList <String>();
aList.add("I");
aList.add("Love");
aList.add("You");
aList.add("Black");
aList.add("Colored");
aList.add("Ferrari");
boolean match;
for(int i = 0; i < aList.size();i++){
match = aList.get(i).equals("Red");
System.out.print(match);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
包含应该可以解决问题
contains should do the trick
一旦找到匹配项,您应该跳出循环,并在循环外打印结果,如下所示:
或者,更短的方法是不使用循环,而是调用 的
contains
方法列表改为:You should break out of the loop once you have found a match, and print the result outside the loop, as shown below:
Alternatively, a shorter approach is to not use a loop but call the
contains
method of the list instead: