- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if a value is present in an Array in Java
At first sort the array −
int intArr[] = {55, 20, 10, 60, 12, 90, 59}; // sorting array Arrays.sort(intArr);
Now, set the value to be searched in an int variable −
int searchVal = 12;
Check for the presence of a value in an array −
int retVal = Arrays.binarySearch(intArr,searchVal); boolean res = retVal > 0 ? true : false;
Following is an example to check if a value is present in an array −
Example
import java.util.Arrays; public class Main { public static void main(String[] args) { // initializing unsorted int array int intArr[] = {55, 20, 10, 60, 12, 90, 59}; // sorting array Arrays.sort(intArr); // let us print all the elements available in list System.out.println("The sorted int array is:"); for (int number : intArr) { System.out.println("Number = " + number); } // entering the value to be searched int searchVal = 12; int retVal = Arrays.binarySearch(intArr,searchVal); boolean res = retVal > 0 ? true : false; System.out.println("Is element 12 in the array? = " + res); System.out.println("The index of element 12 is : " + retVal); } }
Output
The sorted int array is: Number = 10 Number = 12 Number = 20 Number = 55 Number = 59 Number = 60 Number = 90 Is element 12 in the array? = true The index of element 12 is : 1
- Related Articles
- C# program to check if a value is in an array
- How do you check if an element is present in a list in Java?
- Java Program to Check if An Array Contains a Given Value
- Check if a key is present in every segment of size k in an array in Python
- Check if a key is present in every segment of size k in an array in C++
- Java Program to Check if An Array Contains the Given Value
- Java program to check if a Substring is present in a given String
- Check if a value exists in an array and get the next value JavaScript
- How to Check if an Array is Empty or Not in Java
- Golang Program to Check if An Array Contains a Given Value
- Python – Check if particular value is present corresponding to K key
- Is there any way to check if there is a null value in an object or array in JavaScript?
- How to check if a variable is an array in JavaScript?
- Check if an array is stack sortable in C++
- Check if element is present in tuple in Python

Advertisements