tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Tools and Libs > Google Guava > Primitives > Integer Index Of

Integer Index Of 

Google Guava is a java library with lot of utilities and reusable components. This requires the library guava-10.0.jar to be in classpath. The following example shows using Ints.indexOf() and Ints.lastIndexOf() API. It returns the index of first and last matching element from the given integer array.

File Name  :  
com/bethecoder/tutorials/guava/primitive_tests/IntIndexTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.guava.primitive_tests;

import com.google.common.primitives.Ints;

public class IntIndexTest {

  /**
   @param args
   */
  public static void main(String[] args) {
    int [] values = 12421242};
    int value2Find = 2;
    
    System.out.println("The first index of '" + value2Find + 
        "' in the array : " + Ints.indexOf(values, value2Find));
    System.out.println("The last index of '" + value2Find + 
        "' in the array : " + Ints.lastIndexOf(values, value2Find));
    
    value2Find = 6;
    System.out.println("The first index of '" + value2Find + 
        "' in the array : " + Ints.indexOf(values, value2Find));
    System.out.println("The last index of '" + value2Find + 
        "' in the array : " + Ints.lastIndexOf(values, value2Find));
    
    value2Find = 4;
    System.out.println("The first index of '" + value2Find + 
        "' in the array : " + Ints.indexOf(values, value2Find));
    System.out.println("The last index of '" + value2Find + 
        "' in the array : " + Ints.lastIndexOf(values, value2Find));
  }
}
   

It gives the following output,
The first index of '2' in the array : 1
The last index of '2' in the array : 7

The first index of '6' in the array : -1
The last index of '6' in the array : -1

The first index of '4' in the array : 2
The last index of '4' in the array : 6



 
  


  
bl  br