List Java contains doesn't work as expected
I have this code not working as expexted:
import java.util.Arrays;
import java.util.List;
public class Test {
static int[] listm = { 2,3,5 };
public static void main(String[] args) {
contains(listm, 2);
}
public static boolean contains(int[] array, int key) {
List l = Arrays.asList(array);
boolean contain = l.contains(key);
System.out.println("" + contain);
return contain;
}
}
it returns false,even the item 2 is found on the collection listm .
To resolve this problem,we must change the generic value of the collection to Integer.
The code become like this.
public class Test {
static Integer[] listm = { 2,3,5 };
public static void main(String[] args) {
contains(listm, 2);
}
public static boolean contains(Integer[] array, int key) {
List<Integer> l = Arrays.asList(array);
boolean contain = l.contains(key);
System.out.println("" + contain);
return contain;
}
}
it works :)