The String equals() method compares the two given strings based on the content of the string. If any character is not matched, it returns false. If all characters are matched, it returns true. For your scenario, you would look through every String in your List, compare it with the String you want and return true if they are the same.
The ArrayList.contains(Object) method returns true if this List contains the specified element. The actual implementation of the contains method in java.util.ArrayList
is the following:
/**
* Returns true iff element is in this ArrayList.
*
* @param e the element whose inclusion in the List is being tested
* @return true if the list contains e
*/
public boolean contains(Object e)
{
return indexOf(e) != -1;
}
The indexOf()
method of ArrayList returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
Whatever approach you decide to take, either is acceptable. In specifically your case, since you have a hard-coded value that you are checking, in my opinion it would look neater to use the contains method, but that is not a single correct answer.