list.contains() Determine if there is a specific element and return it as a boolean
public static void main(String[] args) {
String arr[] = {"orange","apple","cherry","melon","grape"};
List<String> list = new ArrayList<>(Arrays.asList(arr));
if(list.contains("banana")) {
System.out.println("Exists");
}else {
System.out.println("not exist");
}
}
list.indexOf() Returns the INDEX number of a particular element as an int, -1 if it does not exist
public static void main(String[] args) {
String arr[] = {"orange","apple","cherry","melon","grape"};
List<String> list = new ArrayList<>(Arrays.asList(arr));
int x = list.indexOf("orange");
System.out.println(x);
if(list.indexOf("orange") != -1) {
System.out.println("Exists");
}else {
System.out.println("not exist");
}
}
Recommended Posts