How to Search Element in Java Array with Example

find index of object in Array
While programming in Java, many times we need to check if an String array contains a particular String or an int array contains a number or not. Though array is an object in Java but it does not provide any convenient method to perform these kind of operation. Some time you even need index of an item stored in Array, unfortunately Java API does not provide any direct method. Thanks to open Source Apache Commons provides a utility class called ArrayUtils which allows you to check for an Item in Array, find its index in array, find its lastIndex in Array and perform several other common operations. In fact ArrayUtils has overloaded methods for different kind of Array e.g. String, int, float, double, Object etc, which makes this kind of programming task trivial. In this article we will see an Apache commons example of How to use ArrayUtils class to search an item in Array and find its index.

Java program to check and find index of an String in String array

Java program to find index of object in Array in Java with exampleHere is complete code example of Apache Commons ArrayUtils class which makes checking for an item in array extremely easy, no matter which kind of array it is. 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 index of that item. If you want to do it without Apache commons ArrayUtils class, you can just convert your array to ArrayList by following any of these three method of converting Array to ArrayList and you can leverage contains(), indexOf() and lastIndexOf() method of List class to perform these operations. By the way you need to include Apache commons JAR in your classpath to execute this example, if you are using Maven you can just include Apache common dependency in your pom.xml file

package test;

import java.util.Arrays;
import java.util.List;
import org.apache.commons.lang.ArrayUtils;

 * Java program to check if an Array contains an Item or not
 * and finding index of that item. For example How to check if
 * an String array contains a particular String or not and What is
 * index of that String in Java program.
 * @author

public class ArrayTest{

    public static void main(String args[]) {
      String[] programming = new String[]{"Java", "C++", "Perl", "Lisp"};
      // Checking an String in String Array by converting Array To ArrayList
      List<String> programmingList = Arrays.asList(programming);
      //Checking does this Array contains Java
      boolean result = programmingList.contains("Java");
      System.out.println("Does programming Array contains Java? " + result);
      int index = programmingList.indexOf("Java");
      System.out.println("Index of Java in programming Array is : " + index);
      // Checking item in Array using Apache Commons ArrayUtils class
      System.out.println("Does programming array has Java? " +
                          ArrayUtils.contains(programming, "Java"));
      System.err.println("What is index of Java in array? " +
                          ArrayUtils.indexOf(programming, "Java"));    

Does programming Array contains Java? true
Index of Java in programming Array is : 0
Does programming array has Java? true
What is index of Java in array? 0

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

Other Java Collection Tutorials you may like

No comments:

Post a Comment