Java Vector contains()方法

Java Vector contains()方法

java.util.vector.contains()方法是用来检查一个特定的元素是否存在于向量中。所以基本上它是用来检查一个向量是否包含任何特定的元素。

语法

Vector.contains(Object element)

参数: 该方法需要一个强制性的参数元素,它是矢量类型的。这是需要测试的元素,如果它在向量中存在与否。

返回值: 如果元素存在于向量中,该方法返回True,否则返回False。

以下程序说明了Java.util.Vector.contains()方法。

程序1 :

// Java code to illustrate contains()
import java.util.*;
  
public class VectorDemo {
    public static void main(String args[])
    {
        // Creating an empty Vector
        Vector<String> vec_tor = new Vector<String>();
  
        // Use add() method to add elements into the Vector
        vec_tor.add("Welcome");
        vec_tor.add("To");
        vec_tor.add("Geeks");
        vec_tor.add("4");
        vec_tor.add("Geeks");
  
        // Displaying the Vector
        System.out.println("Vector: " + vec_tor);
  
        // Check for "Geeks" in the Vector
        System.out.println("Does the vector contains 'Geeks'? "
                           + vec_tor.contains("Geeks"));
  
        // Check for "4" in the Vector
        System.out.println("Does the Vector contains '4'? "
                           + vec_tor.contains("4"));
  
        // Check if the Queue contains "No"
        System.out.println("Does the Queue contains 'No'? "
                           + vec_tor.contains("No"));
    }
}

输出:

Vector: [Welcome, To, Geeks, 4, Geeks]
Does the vector contains 'Geeks'? true
Does the Vector contains '4'? true
Does the Queue contains 'No'? false

程序2

// Java code to illustrate contains()
import java.util.*;
  
public class VectorDemo {
    public static void main(String args[])
    {
        // Creating an empty Vector
        Vector<Integer> vec_tor = new Vector<Integer>();
  
        // Use add() method to add elements into the Vector
        vec_tor.add(10);
        vec_tor.add(15);
        vec_tor.add(30);
        vec_tor.add(20);
        vec_tor.add(5);
  
        // Displaying the Vector
        System.out.println("Vector: " + vec_tor);
  
        // Check for "Geeks" in the Vector
        System.out.println("Does the vector contains 'Geeks'? "
                           + vec_tor.contains("Geeks"));
  
        // Check for "4" in the Vector
        System.out.println("Does the Vector contains '4'? "
                           + vec_tor.contains("4"));
  
        // Check if the vector contains "No"
        System.out.println("Does the Vector contains 'No'? "
                           + vec_tor.contains("No"));
    }
}

输出:

Vector: [10, 15, 30, 20, 5]
Does the vector contains 'Geeks'? false
Does the Vector contains '4'? false
Does the Vector contains 'No'? false

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程