7

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());
Alan Coromano
  • 24,958
  • 53
  • 135
  • 205

6 Answers6

12

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).

Ted Hopp
  • 232,168
  • 48
  • 399
  • 521
  • 1
    **It is converting your int[] into a List with a single member: the original list.** --sorry, I didn't get this. – Alan Coromano Jul 07 '13 at 09:39
  • `.asList(num)` means "as list". You should not expect a method called `.asList(num)` to return an array instead... – Costis Aivalis Jul 07 '13 at 10:14
  • 2
    @MariusKavansky - Well, `System.out.println(Arrays.asList(numbers).size());` prints 1 for me. Are you sure you didn't change the declaration of `numbers` to `Integer[]`? – Ted Hopp Jul 07 '13 at 15:13
6

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.

Angelo Fuchs
  • 9,825
  • 1
  • 35
  • 72
2

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

1

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);
fge
  • 119,121
  • 33
  • 254
  • 329
  • Uhm, OK, I need to test; however, if this is a sorted array, my method will work for you. Deleting the irrelevant part. – fge Jul 07 '13 at 11:31
0

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); 
    }
}
saifwilljones
  • 33
  • 2
  • 6
0

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.

TheProgrammer
  • 1,314
  • 5
  • 22
  • 44