Search Element in Array in Java

Let’s see, How can you Search Element in Array. In Java Programming, sometimes we need to check if a String array contains a particular String or an int array contains a number or not.
Java API does not provide any direct method to check element index. We need to use Apache Commons utility class called ArrayUtils. Using ArrayUtils class you can check for an Item in Array, find element’s index in the array.

In this article, we will see  How to use ArrayUtils class to search an item in Array and find its index.

Program to Search Element in Array

Here is an example of Apache Commons ArrayUtils class which makes checking for an item in an array. ArrayUtils has overloaded method for all kinds of array. We will ArrayUtils.contains() and ArrayUtils.indexOf() method to check if Array contains an item and what is an index of that item.

 

Output :

Does programming Array contains Tata? true
Index of Audi in Cars Array is : 1
Does cars array has Audi? true
What is index of Audi in array? 1

 

These were two easy way to check items in Array and finding the index of an Item on Array in Java. Both of these techniques will work in any kind of Array e.g String array or Object array. ArrayUtils even accept primitive arrays e.g. int, float, long etc.

1 Comment
  1. Bhuvan 1:46 PM / September 15, 2016 - Reply

    Thanks to share ….. Please post some information on Array Operations …..

Leave a Reply