Java: use of lastIndexOf with java.util.ArrayList

Post by Jertix, 01/09/2011, 0. Category: Programming

In this article we are going to see an example of lastIndexOf property with an ArrayList of <Strings>

1. Here is the example:

package org.Jertix;

 import java.util.ArrayList; 
   
 public class testlastIndexOf  {  
  
 /** 
  * 
  * @author Jertix  ( Luigi Tedde ) 
  * @url    http://www.jertix.org/
  */ 

  public static void main ( String [  ]  args )   {  
   ArrayList < String >  listNames = new ArrayList < String >  (  ) ; 
   
   String tmpName = "";  
   tmpName="Mark"; 
   listNames.add(tmpName); // index 0 
   
   tmpName="Fred"; 
   listNames.add(tmpName); // index 1 
   
   tmpName="Emily"; 
   listNames.add(tmpName); // index 2 
          
   System.out.print("Last item number: "); 
   System.out.println(listNames.lastIndexOf(tmpName));

   System.out.print("Last item value: "); 
   System.out.println(listNames.get(listNames.lastIndexOf(tmpName))); 
       
   // OUTPUT: 
   // Last item number: 2 
   // Last item value:  Emily 
  
   }  
 } 

Write a comment