contains and indexOf
The last module searched a list with a loop. Two list methods do that searching for you:
list.contains(value)istruewhen the value is in the list, andfalsewhen it isn't.list.indexOf(value)gives the index of the value, or-1when it isn't in the list. If the value is there more than once, you get the first index.
ArrayList<String> colors = new ArrayList<>();
colors.add("red");
colors.add("green");
colors.add("blue");
colors.add("green");
System.out.println(colors.contains("blue"));
System.out.println(colors.contains("pink"));
System.out.println(colors.indexOf("green"));
System.out.println(colors.indexOf("pink"));
true
false
1
-1Both compare with equals, the way you compare Strings yourself, so they also find text the user typed in. The comparison is exact: "Green" with a capital G is not in this list.
They work on number lists too: numbers.contains(7) and numbers.indexOf(7) look for the value 7.
Searching in an if
contains gives a boolean, so the call can be the condition of an if. With indexOf, check for -1 before you use the index:
ArrayList<String> queue = new ArrayList<>();
queue.add("Ines");
queue.add("Tomas");
queue.add("Yuki");
if (queue.contains("Tomas")) {
System.out.println("Tomas is in the queue");
}
String name = "Yuki";
int index = queue.indexOf(name);
if (index == -1) {
System.out.println(name + " is not in the queue");
} else {
System.out.println(name + " has " + index + " people ahead");
}
Tomas is in the queue
Yuki has 2 people aheadYuki is at index 2, and the index is exactly the number of people before her.