You have multiple problems.
Arrays have no contains
method
array0.contains(theNumbers)
You cannot call contains
on an array. Arrays have no such method.
There are several ways to search an array in Java. If your array is sorted, use Arrays.binarySearch
.
Ampersand (&
) flips bits
Your use of &
is not doing what you think. That operator manipulates bits. See The Java Tutorials by Oracle.com, free of cost.
What you meant to do was create a collection of int
values. To do that use an array or use a Collection
object such as List
or Set
. See The Java Tutorials by Oracle to learn about the Java Collections Framework.
Solution
Use a pair of vertical bars for a logical OR operator. Returns true if either or both conditions are true. See The Java Tutorials by Oracle.
int[] arr = { 3, 5, 6 } ;
int x = 2 ;
int y = 3 ;
boolean arrayContainsEitherNumber =
( Arrays.binarySearch( arr , x ) < 0 )
||
( Arrays.binarySearch( arr , y ) < 0 )
;
Get fancy with streams.
int[] arr = { 3 , 5 , 6 };
boolean hit =
Arrays
.stream( arr ) // Returns an `IntStream`, a series of each `int` value in the array.
.filter( i -> ( i == 2 ) || ( i == 3 ) ) // Skips any elements not meeting our criteria.
.findAny() // Halts the stream when first element reaches this point.
.isPresent(); // Returns true if the payload of this optional exists, false if no payload.
System.out.println( "hit = " + hit );
hit = true
Declare a return type
As commented by Dawood ibn Kareem, your method neglected to return the result to the calling code.
Change the void
:
public static void no23( boolean array0 ) { … }
… to boolean
and return your result:
public static boolean no23( boolean array0 ) {
…
return statemnt ;
}
You may detect a theme in this Answer: Study The Java Tutorials by Oracle before posting here.