..and, what the devil is going on here?
int [] numbers1To9 = new int[]{1,2,3,4,5,6,7,8,9};
System.out.println("one is here, true or false?: "+Arrays.asList(numbers1To9).contains(1));
output: one is here, true or false?: false
..and, what the devil is going on here?
int [] numbers1To9 = new int[]{1,2,3,4,5,6,7,8,9};
System.out.println("one is here, true or false?: "+Arrays.asList(numbers1To9).contains(1));
output: one is here, true or false?: false
The best way to find a number in an array It depends on your array, whether it is sorted or not
If your array is sorted Then you will be able to find the num in the array in time running of O(log(N)).
But if your array is not sorted then you will want to consider sorting the array and after There are several algorithms that you can find your number in an array.
Such as binary search and more...
If working with sorted arrays, or when the sort operation to an unsorted array can be considered "cheap", the binarySearch could be considered a good option; It avoids the creation of further collections (such as Lists) as it directly works with the original array, and its mechanism is aimed to find the position (or one of them) where the required key is stored. As a result, you are able to identify its existence(implicit) and the index where is located.
You already have the array sorted, so no need in your case (which is an advantage in order to use this algorithm); Be aware that when using unsorted arrays, calling Arrays.sort before the binarySearch is a must in order to avoid "undefined" results.
For example, if you want to know if the value 1 exists:
/*Arrays.sort(numbers1To9); -- only if unsorted*/
boolean found = (Arrays.binarySearch((numbers1To9), 1))>=0?true:false; //--> true
If you wish to get the position as well, for example, of the value 2:
/*Arrays.sort(numbers1To9); -- only if unsorted*/
int pos = Arrays.binarySearch((numbers1To9), 2); //-->1
boolean found = pos>=0; //--> true
binarySearch will only return a negative output if the element is not found. If the key to be found is duplicated, there's no guarantee of which position from the specified key will return.
Regardless of this, if the result is >=0, the array is guaranteed to contain the number, and the value required is also guaranteed to be stored in the returned index.
The result when no key is found is somehow interesting
The negative result shown if the key is not found follows this logic:
(-(insertion point) - 1). The insertion point is defined as the point at which the key would be inserted into the array: the index of the first element greater than the key, or a.length if all elements in the array are less than the specified key. This guarantees that the return value will be >= 0 if and only if the key is found.
So if you try to find any number bigger than 9, insertion point would be numbers1To9.length -> 9. Hence,10 and INTEGER.MAX_VALUE will output the same position:
int pos = Arrays.binarySearch((numbers1To9), 10); // -(9)-1 --> pos=-10
pos = Arrays.binarySearch((numbers1To9), Integer.MAX_VALUE); // -(9)-1 --> pos=-10
With number 0, insertion point would be 0 (as 1 is bigger and its position in the array is 0):
int pos = Arrays.binarySearch((numbers1To9), 0); // -(0)-1 --> pos=-1
In order to look at how bad binarySearch works with unsorted arrays:
int [] numberUnsorted= new int[]{1,2,4,9,7,6,5,8,3};
int pos = Arrays.binarySearch((numberUnsorted), 3); //--> pos = -3 (FAIL)
pos = Arrays.binarySearch((numberUnsorted), 9); //--> pos = -10 (FAIL)
pos = Arrays.binarySearch((numberUnsorted), 6); //--> pos = -4 (FAIL)
So calling them "undefined" is a really benevolent therm
Note that the binarySearch would be "one of the best practices" for finding
a number in an array, with the condition of the array being sorted. In other scenarios, where the array is unsorted, you may be aware of the complexity of sorting it and decide wether another mechanism that doesn't need the sort operation would be a better approach. It will depend on the array type, size, and values. There usually are no "best deffinitive ways" without knowing the specific context of the search
Alternative to finding the number in the int[] yourself, and close to your own attempt, you can convert the int[] to a List fairly easily:
int [] numbers1To9 = new int[]{1,2,3,4,5,6,7,8,9};
List<Integer> ilist = IntStream.of(numbers1To9).boxed().collect(Collectors.toList());
System.out.println("one is here, true or false?: "+ ilist.contains(1));
Output:
one is here, true or false?: true
EDIT
As was mentioned in the comment below, if your only goal is to check the presence of a value, you don't need to box and collect at all:
boolean contains1 = IntStream.of(numbers1To9).anyMatch(i -> i == 1);
System.out.println("one is here, true or false?: "+ contains1);