How Arrays.asList (...) works. Contains (...)?

I have the following code.

    String[] weekEnds={"0","6"};

    for(int i=0;i<7;i++){

    if(Arrays.asList(weekEnds).contains(i)){

     System.out.println("I am in the array");       
    }    
  }

but it is not included in the if condition .. I don’t know where this is happening. Thanks at Advance ..

+5
source share
5 answers

Or change the condition ifto

if(Arrays.asList(weekEnds).contains(String.valueOf(i))){

Or change the array to

Integer[] weekEnds={0,6};
+10
source

You are comparing strings and ints. Your array contains strings ( "0", "6"), but you pass int to the comparison.

This int will be in a box in Integer but Integer(1)not equal"1"

+7
source

( String Integer s).

You are converting an array Stringto List. Then you call contains()on it with the value int. Since the method accepts Objectas a parameter type, intgets autoboxed in Integer.

To fix this, write:

if(Arrays.asList(weekEnds).contains("" + i)) {
+3
source

You need to have an int array in order to be able to perform such a comparison.

Change this:

String[] weekEnds={"0","6"};

to

int[] weekEnds = {0,6};
+3
source

Just change to Integer[] weekEnds={0,6};, and I think it will work.

0
source

All Articles