Java Program to check if Integer element is present in an array
Program
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class CheckIntegerArrayForValuePresent {
public static void main(String[] args) {
System.out.println("Enter the required size of the array: ");
Scanner reader = new Scanner(System.in);
int size = reader.nextInt();
Integer inputArray[] = new Integer[size];
System.out.println("Enter the elements of the array: ");
for (int i = 0; i < size; i++) {
inputArray[i] = reader.nextInt();
}
System.out.println("Enter the element you want to search: ");
Integer searchElement = reader.nextInt();
List<Integer> inputList = new ArrayList<>(Arrays.asList(inputArray));
if (inputList.contains(searchElement)) {
System.out.println("Element is present");
} else {
System.out.println("Element is not present");
}
}
}
Output 1
javac .\CheckIntegerArrayForValuePresent.java
java CheckIntegerArrayForValuePresent
Enter the required size of the array:
5
Enter the elements of the array:
23
44
12
56
79
Enter the element you want to search:
79
Element is present
Output 2
javac .\CheckIntegerArrayForValuePresent.java
java CheckIntegerArrayForValuePresent
Enter the required size of the array:
4
Enter the elements of the array:
29
77
45
10
Enter the element you want to search:
44
Element is not present