Thursday 8 May 2014

Vector : indexOf(Object o) : get the index of Element

public int indexOf(Object o)
Returns the index of the first occurrence of the specified element in this vector, or -1 if this vector does not contain the element.

import java.util.*;

class VectorIndexOf{
 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.println("Hash Code is");
  System.out.println(myVector.hashCode());  
  
  System.out.print("\nIndex of 12 is ");
  System.out.println(myVector.indexOf(12));
  System.out.print("\nIndex of 100 is ");
  System.out.println(myVector.indexOf(100));
 }
}

Output
Elements in myVector
[10, 11, 12, 10, 12]
Hash Code is
38203916

Index of 12 is 2

Index of 100 is -1

Prevoius                                                 Next                                                 Home

No comments:

Post a Comment