Get element index

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.
int indexOf(Object o, int index)
Returns the index of the first occurrence of the specified element in this vector, searching forwards from index, or returns -1 if the element is not found.
int lastIndexOf(Object o)
Returns the index of the last occurrence of the specified element in this vector, or -1 if this vector does not contain the element.
int lastIndexOf(Object o, int index)
Returns the index of the last occurrence of the specified element in this vector, searching backwards from index, or returns -1 if the element is not found.

import java.util.Vector;

public class Main{

  public static void main(String args[]) {

    Vector v = new Vector();

    v.add("java2s.com");
    v.add("java2s.com");
    v.add("java2s.com");
    
    System.out.println(v.indexOf("java2s.com"));
    System.out.println(v.lastIndexOf("java2s.com"));
  }
}

The output:


0
2

Search an element of Vector from specific index


import java.util.Vector;

public class Main {
  public static void main(String[] args) {
    Vector v = new Vector();
    v.add("1");
    v.add("2");
    v.add("3");
    v.add("4");
    v.add("java2s.com");
    v.add("1");
    v.add("2");
    int index = v.indexOf("1", 4);
    if (index == -1)
      System.out.println("Vector does not contain 1 after index # 4");
    else
      System.out.println("Vector contains 1 after index # 4 at index #" + index);

    int lastIndex = v.lastIndexOf("2", 5);
    
    if (lastIndex == -1)
      System.out.println("not contain 2 after index # 5");
    else
      System.out.println("Last occurrence of 2 after index 5 #" + lastIndex);
  }
}

The output:


Vector contains 1 after index # 4 at index #5
Last occurrence of 2 after index 5 #1
Home 
  Java Book 
    Collection  

Vector:
  1. Vector class
  2. Create Vector objects
  3. Add elements to this vector
  4. Size and capacity
  5. Remove all elements from a vector
  6. Clone a vector
  7. Does it contain a certain element
  8. Copy elements in vector to an array
  9. Get Enumeration from this vector
  10. Compare two vectors
  11. Get element from vector
  12. Is vector empty
  13. Get element index
  14. Remove element from a vector
  15. Retain elements collection c has
  16. Replace element in a vector
  17. Get a sub list from a list
  18. Convert Vector to Array