Does it contain a certain element

ReturnMethodSummary
booleancontains(Object o)Returns true if this vector contains the specified element.
boolean containsAll(Collection<?> c) Returns true if this Vector contains all of the elements in the specified Collection.

import java.util.Vector;

public class Main{

  public static void main(String args[]) {

    Vector v = new Vector(2000);

    v.add("java2s.com");

    boolean b = v.contains("java2s.com");
    
    System.out.println(b);
    
  }
}

The output:


true

Search an element of Vector


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("5");
    v.add("1");
    v.add("java2s.com");
    boolean blnFound = v.contains("3");
    System.out.println("contain 3: " + blnFound);
    int index = v.indexOf("5");
    if (index == -1)
      System.out.println("does not contain 5");
    else
      System.out.println("5 at index :" + index);
    int lastIndex = v.lastIndexOf("2");
    if (lastIndex == -1)
      System.out.println("does not contain 2");
    else
      System.out.println("Last occurrence of 2:" + lastIndex);
  }
}

The output:


contain 3: true
5 at index :4
Last occurrence of 2:1
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.