Skip to content
Advertisement

Check if a String is in an ArrayList of Strings

How can I check if a String is there in the List?

I want to assign 1 to temp if there is a result, 2 otherwise.

My current code is:

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;

Advertisement

Answer

temp = bankAccNos.contains(no) ? 1 : 2;
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement