xxxxxxxxxx
// Better solution
public static boolean contains(final int[] arr, final int key) {
return Arrays.stream(arr).anyMatch(i -> i == key);
}
xxxxxxxxxx
String[] fieldsToInclude = { "id", "name", "location" };
if ( ArrayUtils.contains( fieldsToInclude, "id" ) ) {
// Do some stuff.
}
xxxxxxxxxx
public boolean contains(final int[] array, final int key) {
return ArrayUtils.contains(array, key);
}
//Note: It's worth noting that ArrayUtils.contains()
//is part of Apache Commons Lang library. Even though that's a
// great library, it is probably still not a good idea to
// add external dependency just to check if array contains an element.
xxxxxxxxxx
// Convert to stream and test it
boolean result = Arrays.stream(alphabet).anyMatch("A"::equals);
if (result) {
System.out.println("Hello A");
}
Copy
xxxxxxxxxx
import java.util.*;
import java.io.*;
public class Array1 {
public static void main(String[]args)
{
Scanner sc=new Scanner(System.in);
int size=sc.nextInt();
int []arr=new int[size];
for(int i=0;i<arr.length;i++)
{
arr[i]=sc.nextInt();
}
int find=sc.nextInt();
int count=0;
for(int i=0;i<arr.length;i++)
{
if(arr[i]==find)
count++;
}
if(count>0)
{
System.out.println("Element is found");
}
else
System.out.println("Element is not founded");
}
}