Reputation: 2800
I have a string[]
String [] example = new String[]{
"one",
"two",
"three",
"four"
}
i am trying to check if this array contain next string "one", but it return false... Here is my code:
boolean check = Arrays.asList(example).contains("one");
any ideas why it happens?
Upvotes: 2
Views: 1328
Reputation: 2727
If you look at the Javadoc for List at the contains method you will see that it uses the equals() method to evaluate if two objects are the same.
Make test:
List<String> list = Arrays.asList(example);
boolean check = list.contains(list.get(1));
and
List<String> list = Arrays.asList(example);
boolean check = list.contains("one");
Upvotes: 1
Reputation: 712
Maybe this is somehow related to the fact, that Arrays.asList()
returns special implementation of List - java.util.Arrays.ArrayList
, and not the java.util.ArrayList
.
Upvotes: 0
Reputation: 116
As this code is correct I guess that at the time/point of using this code your Array may have not been initialized yet or it's contents may have been altered.
Also try a clean and rebuild.
Upvotes: 0