java.util.vector.contains()方法用于检查Vector中是否存在特定元素。因此,本质上,它用于检查向量是否包含任何特定元素。
用法:
Vector.contains(Object element)
参数:此方法采用矢量类型的必需参数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
相关用法
- Java Vector set()用法及代码示例
- Java Vector add()用法及代码示例
- Java Vector get()用法及代码示例
- Java Vector removeAllElements()用法及代码示例
- Java Vector setElementAt()用法及代码示例
- Java Vector ensureCapacity()用法及代码示例
- Java Vector containsAll()用法及代码示例
- Java Vector copyInto()用法及代码示例
- Java Vector lastElement()用法及代码示例
- Java Vector removeAll()用法及代码示例
- Java Vector remove()用法及代码示例
- Java Vector removeElementAt()用法及代码示例
- Java Vector clear()用法及代码示例
- Java Vector addAll()用法及代码示例
- Java Vector removeIf()用法及代码示例
注:本文由纯净天空筛选整理自kundankumarjha大神的英文原创作品 Vector contains() Method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。