Stan Malcolm
Stan Malcolm

Reputation: 2800

Arrays.asList(array).contains(value) doesn't work correctly (android)

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

Answers (3)

dieter_h
dieter_h

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

endasan
endasan

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

Steffen H
Steffen H

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

Related Questions