find an index of the object in Array
While programming in Java, many times we need to check if a 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 kinds of operation. Sometimes you even need an 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 the 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 a String in String array

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 methods 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
* a String array contains a particular String or not and What is
* index of that String in Java program.
*
* @author http://java67.blogspot.com
*/
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? " +
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
* a String array contains a particular String or not and What is
* index of that String in Java program.
*
* @author http://java67.blogspot.com
*/
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? " +
System.err.println("What is index of Java in array? " +
ArrayUtils.indexOf(programming, "Java"));
}
}
Output
Does programming Array contain Java? true
Index of Java in programming Array is : 0
Does programming array has Java? true
What is the index of Java in the array? 0
}
}
Output
Does programming Array contain Java? true
Index of Java in programming Array is : 0
Does programming array has Java? true
What is the index of Java in the array? 0
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.
Further Learning
Java In-Depth: Become a Complete Java Engineer
Java Fundamentals: Collections
Data Structures and Algorithms: Deep Dive Using Java
Other Java Collection Tutorials you may like
how can add time for lnear search?
ReplyDeletecan you please clarify what do you mean? linear search is O(n) time operation.
Delete