Another option if you are using Guava Collections is Ints.indexOf
// Perfect storm:
final int needle = 42;
final int[] haystack = [1, 2, 3, 42];
// Spoiler alert: index == 3
final int index = Ints.indexOf(haystack, needle);
This is a great choice when space, time and code reuse are at a premium. It is also very terse.
public class Test {
public static int Tab[] = {33,44,55,66,7,88,44,11,23,45,32,12,95};
public static int search = 23;
public static void main(String[] args) {
long stop = 0;
long time = 0;
long start = 0;
start = System.nanoTime();
int index = getIndexOf(search,Tab);
stop = System.nanoTime();
time = stop - start;
System.out.println("equal to took in nano seconds ="+time);
System.out.println("Index of searched value is: "+index);
System.out.println("De value of Tab with searched index is: "+Tab[index]);
System.out.println("==========================================================");
start = System.nanoTime();
int Bindex = bitSearch(search,Tab);
stop = System.nanoTime();
time = stop - start;
System.out.println("Binary search took nano seconds ="+time);
System.out.println("Index of searched value is: "+Bindex);
System.out.println("De value of Tab with searched index is: "+Tab[Bindex]);
}
public static int getIndexOf( int toSearch, int[] tab ){
int i = 0;
while(!(tab[i] == toSearch) )
{ i++; }
return i; // or return tab[i];
}
public static int bitSearch(int toSearch, int[] tab){
int i = 0;
for(;(toSearch^tab[i])!=0;i++){
}
return i;
}
/**
* Method to get the index of the given item from the list
* @param stringArray
* @param name
* @return index of the item if item exists else return -1
*/
public static int getIndexOfItemInArray(String[] stringArray, String name) {
if (stringArray != null && stringArray.length > 0) {
ArrayList<String> list = new ArrayList<String>(Arrays.asList(stringArray));
int index = list.indexOf(name);
list.clear();
return index;
}
return -1;
}
In the main method using for loops:
-the third for loop in my example is the answer to this question.
-in my example I made an array of 20 random integers, assigned a variable the smallest number, and stopped the loop when the location of the array reached the smallest value while counting the number of loops.
import java.util.Random;
public class scratch {
public static void main(String[] args){
Random rnd = new Random();
int randomIntegers[] = new int[20];
double smallest = randomIntegers[0];
int location = 0;
for(int i = 0; i < randomIntegers.length; i++){ // fills array with random integers
randomIntegers[i] = rnd.nextInt(99) + 1;
System.out.println(" --" + i + "-- " + randomIntegers[i]);
}
for (int i = 0; i < randomIntegers.length; i++){ // get the location of smallest number in the array
if(randomIntegers[i] < smallest){
smallest = randomIntegers[i];
}
}
for (int i = 0; i < randomIntegers.length; i++){
if(randomIntegers[i] == smallest){ //break the loop when array location value == <smallest>
break;
}
location ++;
}
System.out.println("location: " + location + "\nsmallest: " + smallest);
}
}
Code outputs all the numbers and their locations, and the location of the smallest number followed by the smallest number.
Binary search: Binary search can also be used to find the index of the array element in an array. But the binary search can only be used if the array is sorted. Java provides us with an inbuilt function which can be found in the Arrays library of Java which will rreturn the index if the element is present, else it returns -1. The complexity will be O(log n).
Below is the implementation of Binary search.
public static int findIndex(int arr[], int t) {
int index = Arrays.binarySearch(arr, t);
return (index < 0) ? -1 : index;
}
The easiest way is to iterate. For example we want to find the minimum value of array and it's index:
public static Pair<Integer, Integer> getMinimumAndIndex(int[] array) {
int min = array[0];
int index = 0;
for (int i = 1; i < array.length; i++) {
if (array[i] < min) {
min = array[i];
index = i;
}
return new Pair<min, index>;
This way you test all array values and if some of them is minimum you also know minimums index.
It can work the same with searching some value:
public static int indexOfNumber(int[] array) {
int index = 0;
for (int i = 0; i < array.length; i++) {
if (array[i] == 77) { // here you pass some value for example 77
index = i;
}
}
return index;
}
I see a lot of solutions here that are not working. The most upvoted solution used an array of Integer, instead of offering a solution to an int array. It is as simple as this: