检查字符串是否在字符串的 ArrayList 中 [英] Check if a String is in an ArrayList of Strings
本文介绍了检查字符串是否在字符串的 ArrayList 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何检查List
中是否存在String
?
如果有结果,我想将 1
分配给 temp
,否则将 2
分配给 temp
.
I want to assign 1
to temp
if there is a result, 2
otherwise.
我当前的代码是:
Integer temp = 0;
List<String> bankAccNos = new ArrayList<String>();//assume list contains values
String bankAccNo = "abc";
for(String no : bankAccNos)
if(no.equals(bankAccNo))
temp = 1;
推荐答案
temp = bankAccNos.contains(no) ? 1 : 2;
这篇关于检查字符串是否在字符串的 ArrayList 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文