Java中的Vector contains()方法

这个 JAVAutil。矢量。包含() 方法用于检查向量中是否存在特定元素。所以基本上它是用来检查向量是否包含任何特定的元素。

null

语法:

Vector.contains(Object element)

参数: 此方法采用强制参数 要素 这是一种向量。这是需要测试的元素,如果它是否存在于向量中。

返回值: 此方法返回 符合事实的 如果元素存在于向量中,则返回 错误的 .

下面的程序演示了Java。util。矢量。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

© 版权声明
THE END
喜欢就支持一下吧
点赞13 分享