A simple Java code for checking whether an element exists in an array or not:
import java.util.Arrays;
public class Main {
static int[] numbers = {813, 907, 908, 909, 910};
public static void main(String[] args) {
int number = 907;
//Integer number = 907; // the same thing -- it's not found.
boolean b = Arrays.asList(numbers).contains(number);
System.out.println(b); // => false
}
}
1) Why doesn't it find 907 in the array?
2) If there is a better way of doing it, go ahead and share your knowledge.
UPDATE:
It was said that asList
converts your int[]
into a List<int[]>
with a single member: the original list. However, I expect the following code to give me 1, but it gives me 5:
System.out.println(Arrays.asList(numbers).size());