Checking for an element in an array

Simple Java code to check if 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 not found. boolean b = Arrays.asList(numbers).contains(number); System.out.println(b); // => false } } 

1) Why doesn't he find 907 in the array?

2) If there is a better way to do this, go ahead and share your knowledge.

UPDATE:

It was said that asList converts your int[] to List<int[]> with one 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()); 
+6
source share
6 answers

The problem is that Arrays.asList(numbers) does not do what you think. It converts your int[] to List<int[]> with one 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 check if the result is negative (this means that it was not found).

+12
source

Lists do not contain primitives, so Arrays.asList (int []) will create 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, what will Arrays.asList(numbers) contain as long as it is 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 the following result:

 false list: [[ I@da89a7 ] content: [813, 907, 908, 909, 910] 

As you can see, List contains one element of type int[] ( [[I indicates int[] ). He has elements that were originally created.

+6
source

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

+2
source

Since your array is sorted, you can use Arrays.binarySearch() .

This allows you not to convert the List first. Check the return code of this method: if it is positive, the element is in an array; if it is negative, it is not:

 int number = 907; System.out.println(Arrays.binarySearch(numbers, number) >= 0); 
+1
source

Use this code. This is just one of those times when you have to deal with Java 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); } } 
0
source

You can simply use the for loop through the array and map each element to the one you are looking for. Subsequently, you can go from there.

For this reason, it does not work for the same reason as others.

0
source

Source: https://habr.com/ru/post/948880/


All Articles