Trabla: Java: check if string in string array
Solving:
1. Convert String[] array to List<String> using method Arrays.asList
2. Use List method contains
Example:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Test{
public static void main(String[] args) {
String[] cities = { "New York", "London", "Odessa" };
List<String> citiesList = Arrays.asList( cities );
if( citiesList.contains( "Odessa" ) ){
System.out.println( "Hooray!!! Odessa is in list." );
}
}
}
No comments:
Post a Comment