Reputation: 26008
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());
Upvotes: 7
Views: 35743
Reputation: 1344
You could just use a for loop going through the array and match each element with the one that you are looking for. Afterwards you can go from there.
For the reason why it isnt working is the same reason as others said before.
Upvotes: 0
Reputation: 9941
Lists don't contain primitives, so Arrays.asList(int[]) will produce a List
with one entry of type int[]
.
This code works:
static Integer[] numbers = {813, 907, 908, 909, 910};
public static void main(String[] args) {
Integer number = 907;
boolean b = Arrays.asList(numbers).contains(number);
System.out.println(b); // => false
}
For your question as what will Arrays.asList(numbers)
contain as long as it is an int[]
:
This code:
static int[] numbers = {813, 907, 908, 909, 910};
public static void main(String[] args) {
int number = 907;
List<int[]> list = Arrays.asList(numbers);
boolean b = list.contains(number);
System.out.println(b); // => false
System.out.println("list: " + list);
for(int[] next : list) {
System.out.println("content: " + Arrays.toString(next));
}
}
has this result:
false
list: [[I@da89a7]
content: [813, 907, 908, 909, 910]
As you can see, the list
contains one element of type int[]
(the [[I
indicate the int[]
). It has the elements that were initially created.
Upvotes: 6
Reputation: 121712
Since your array is sorted, you can use Arrays.binarySearch()
.
This allows you not to have to convert to a List
first. Check the return code of this method: if it is positive, the element is in the array; if it is negative, it isn't:
int number = 907;
System.out.println(Arrays.binarySearch(numbers, number) >= 0);
Upvotes: 1
Reputation: 43
Use this code instead. This is just one of those times you've got to deal with Java's strongly typed quirks :)
import java.util.Arrays;
public class Main {
static Integer[] numbers = {813, 907, 908, 909, 910};
public static void main(String[] args) {
Integer number = new Integer(907);
boolean b = Arrays.asList(numbers).contains(number);
System.out.println(b);
}
}
Upvotes: 0
Reputation: 234795
The problem is that Arrays.asList(numbers)
isn't doing what you think. It is converting your int[]
into a List<int[]>
with a single member: the original list.
You can do a simple linear search or, if your numbers
array is always sorted, use Arrays.binarySearch(numbers, 907);
and test whether the result is negative (meaning not found).
Upvotes: 12
Reputation:
With guava ImmutableSet
:
public class Main {
private static final Set<Integer> NUMBERS = ImmutableSet.of(813, 907, 908, 909, 910);
public static void main(final String[] args) {
final int number = 907;
final boolean b = NUMBERS.contains(number);
System.out.println(b); // true
}
}
ImmutableSet
ensures no one adds something to NUMBERS
Upvotes: 2