Thursday 8 May 2014

Vector : lastIndexOf : Get the Last Index of Element

public synchronized int lastIndexOf(Object o)
Returns the index of the last occurrence of the specified element or -1 if this vector does not contain the element.

import java.util.*;

class VectorLastIndexOf{
 public static void main(String args[]){
  Vector<Integer> myVector;
  myVector = new Vector<> ();
  
  /*Add Elements to myVector*/
  myVector.add(10);
  myVector.add(11);
  myVector.add(12);
  myVector.add(10);
  myVector.add(12);
  
  System.out.println("Elements in myVector");
  System.out.println(myVector); 
  
  System.out.print("\nLast Index of 12 is ");
  System.out.println(myVector.lastIndexOf(12));
  System.out.print("\nLast Index of 100 is ");
  System.out.println(myVector.lastIndexOf(100));
 }
}

Output
Elements in myVector
[10, 11, 12, 10, 12]

Last Index of 12 is 4

Last Index of 100 is -1



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment