Java Vector contains() 方法



描述

Java Vector contains(Object elem) 方法用于测试向量中是否存在某个元素。

声明

以下是 java.util.Vector.contains() 方法的声明

public boolean contains(Object elem)

参数

elem − 这是作为输入的对象。

返回值

true − 当且仅当指定对象与此向量中的某个组件相同时,返回 true。否则返回 false。

异常

  • ClassCastException − 如果指定元素的类型与此集合不兼容(可选)。

  • NullPointerException − 如果指定元素为 null,并且此集合不支持 null 元素(可选)。

检查整数向量中元素是否存在示例

以下示例演示了 Java Vector contains() 方法的使用。我们使用的是 Integer。我们将添加一些元素,然后检查是否存在某个元素。

package com.tutorialspoint;

import java.util.Vector;

public class VectorDemo {
   public static void main(String[] args) {

      // create an empty vector
      Vector<Integer> vector = new Vector<>();

      // use add() method to add elements in the vector
      vector.add(20);
      vector.add(30);
      vector.add(10);
      vector.add(18);        

      // let us print all the elements available in vector
      System.out.println("Vector = " + vector);

      // vector contains element 10
      if (vector.contains(10)) {
         System.out.println("element 10 is present in the vector");
      } else {
         System.out.println("element 10 is not present in the vector");
      }

      // vector does not contain element 25
      if (vector.contains(25)) {
         System.out.println("element 25 is present in the vector");
      } else {
         System.out.println("element 25 is not present in the vector");    
      }
   }
}

输出

让我们编译并运行上述程序,这将产生以下结果:

Vector = [20, 30, 10, 18]
element 10 is present in the vector
element 25 is not present in the vector

检查字符串向量中元素是否存在示例

以下示例演示了 Java Vector contains() 方法与字符串一起使用的情况。我们将添加一些元素,然后检查是否存在某个元素。

package com.tutorialspoint;

import java.util.Vector;

public class VectorDemo {
   public static void main(String[] args) {

      // create an empty vector
      Vector<String> vector = new Vector<>();

      // use add() method to add elements in the vector
      vector.add("Welcome");
      vector.add("to");
      vector.add("tutorialspoint");
      vector.add(".com");        

      // let us print all the elements available in vector
      System.out.println("Vector = " + vector);

      // vector contains element tutorialspoint
      if (vector.contains("tutorialspoint")) {
         System.out.println("element tutorialspoint is present in the vector");
      } else {
         System.out.println("element tutorialspoint is not present in the vector");
      }

      // vector does not contain element html
      if (vector.contains("html")) {
         System.out.println("element html is present in the vector");
      } else {
         System.out.println("element html is not present in the vector");    
      }
   }
}

输出

让我们编译并运行上述程序,这将产生以下结果:

Vector = [Welcome, to, tutorialspoint, .com]
element tutorialspoint is present in the vector
element html is not present in the vector

检查对象向量中元素是否存在示例

以下示例演示了 Java Vector contains() 方法与 Student 对象一起使用的情况。我们将添加一些元素,然后检查是否存在某个元素。

package com.tutorialspoint;

import java.util.Vector;

public class VectorDemo {
   public static void main(String[] args) {

      // create an empty vector
      Vector<Student> vector = new Vector<>();

      // use add() method to add elements in the vector
      vector.add(new Student(1, "Julie"));
      vector.add(new Student(2, "Robert"));
      vector.add(new Student(3, "Adam"));      

      // let us print all the elements available in vector
      System.out.println("Vector = " + vector);

      // vector contains element Robert
      if (vector.contains(new Student(2, "Robert"))) {
         System.out.println("Student Robert is present in the vector");
      } else {
         System.out.println("Student Robert is not present in the vector");
      }

      // vector does not contain element Jane
      if (vector.contains(new Student(4, "Jane"))) {
         System.out.println("Student Jane is present in the vector");
      } else {
         System.out.println("Student Jane is not present in the vector");    
      }
   }
} 
class Student {
   int rollNo;
   String name;

   Student(int rollNo, String name){
      this.rollNo = rollNo;
      this.name = name;
   }

   @Override
   public String toString() {
      return "[ " + this.rollNo + ", " + this.name + " ]";
   }
   
   @Override
   public boolean equals(Object obj) {
      Student s = (Student)obj;
      return this.rollNo == s.rollNo && this.name.equalsIgnoreCase(s.name);
   }
}

输出

让我们编译并运行上述程序,这将产生以下结果:

Vector = [[ 1, Julie ], [ 2, Robert ], [ 3, Adam ]]
Student Robert is present in the vector
Student Jane is not present in the vector
java_util_vector.htm
广告