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
廣告