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 integer array contains a number or not. Though array is an object in Java but it does not provide any convenient method to perform searching elements or finding elements. 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 kinds of Array like 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.
Btw, if you find it on your own, you can also use binary search and linear search algorithm to scan array and check if a given element exists on array or not. You can also find their index or position in the array.
Java program to check and find an index of element in a String array
Here 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 an 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 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
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 like String array or Object array. ArrayUtils even accept primitive arrays e.g. int, float, long etc.
Related Data Structure and Algorithm Interview Questions from Javarevisited Blog
- Top 15 Data Structure and Algorithm Interview Questions (see here)
- How to reverse an array in place in Java? (solution)
- How to remove duplicate elements from an array in Java? (solution)
- How to check if a number is binary in Java? (answer)
- Top 30 Array Coding Interview Questions with Answers (see here)
- How to find all pairs whose sum is equal to a given number in Java (solution)
- Top 30 linked list coding interview questions (see here)
- 100+ Data Structure Coding Problems from Interviews (questions)
- Write a Program to remove duplicates from an array without using Collection API? (program)
- How to reverse String in Java without using API methods? (Solution)
- Write a method to remove duplicates from ArrayList in Java? (Solution)
- Top 50 Java Programs from Coding Interviews (see here)
- 5 Free Data Structure and Algorithms Courses for Programmers (courses)
- How to compare two arrays in Java? (answer)
- Top 20 String coding interview questions (see here)
- 10 Algorithms Books Every Programmer Should Read (books)
- 10 Free Data Structure and Algorithm Courses for Programmers (courses)
- How to find a missing number in an array? (answer)
Thanks for reading this article so far. If you like this Java Array Tutorial then please share it with your friends and colleagues. If you have any questions or doubt then please let us know and I'll try to find an answer for you. As always suggestions, comments, innovative and better answers are most welcome.
P. S. - If you are looking for some Free Data Structure and Algorithms courses to improve your understanding of Data Structure and Algorithms, then you should also check the Data Structure in Java free course on Udemy. It's completely free and all you need to do is create a free Udemy account to enroll in this course.
how can add time for lnear search?
ReplyDeletecan you please clarify what do you mean? linear search is O(n) time operation.
Delete