当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java Vector equals()用法及代码示例


Java中Vector类的java.util.vector.equals(Object obj)方法用于验证Object与vector的相等性并进行比较。仅当两个Vector包含相同顺序的相同元素时,列表才返回true。

用法:

first_vector.equals(second_vector)

参数:此方法接受强制性参数second_vector,该参数表示要与第一个Vector进行比较的第二个Vector。


返回值:如果相等成立且对象和向量均相等,则该方法返回true,否则返回false。

下面的程序用于说明java.util.Vector.elements()方法的用法:

示例1:

// Java code to illustrate the equals() method 
import java.util.*; 
  
public class Vector_Demo { 
    public static void main(String[] args) 
    { 
  
        // Creating an empty Vector 
        Vector<String> vec_tor1 = new Vector<String>(5); 
  
        // Inserting elements into the table 
        vec_tor1.add("Geeks"); 
        vec_tor1.add("4"); 
        vec_tor1.add("Geeks"); 
        vec_tor1.add("Welcomes"); 
        vec_tor1.add("You"); 
  
        // Displaying the Vector 
        System.out.println("The Vector is: "
                           + vec_tor1); 
  
        // Creating an empty Vector 
        Vector<String> vec_tor2 = new Vector<String>(5); 
  
        // Inserting elements into the table 
        vec_tor2.add("Geeks"); 
        vec_tor2.add("4"); 
        vec_tor2.add("Geeks"); 
        vec_tor2.add("Welcomes"); 
        vec_tor2.add("You"); 
  
        // Displaying the Vector 
        System.out.println("The Vector is: "
                           + vec_tor2); 
  
        System.out.println("Are both of them equal? "
                           + vec_tor1.equals(vec_tor2)); 
    } 
}
输出:
The Vector is: [Geeks, 4, Geeks, Welcomes, You]
The Vector is: [Geeks, 4, Geeks, Welcomes, You]
Are both of them equal? true

示例2:

// Java code to illustrate the equals() method 
import java.util.*; 
  
public class Vector_Demo { 
    public static void main(String[] args) 
    { 
  
        // Creating an empty Vector 
        Vector<Integer> vec_tor1 = new Vector<Integer>(5); 
  
        // Inserting elements into the table 
        vec_tor1.add(10); 
        vec_tor1.add(15); 
        vec_tor1.add(20); 
        vec_tor1.add(25); 
        vec_tor1.add(30); 
  
        // Displaying the Vector 
        System.out.println("The Vector is: " + vec_tor1); 
  
        // Creating an empty Vector 
        Vector<Integer> vec_tor2 = new Vector<Integer>(6); 
  
        // Inserting elements into the table 
        vec_tor2.add(10); 
        vec_tor2.add(15); 
        vec_tor2.add(20); 
        vec_tor2.add(25); 
        vec_tor2.add(30); 
        vec_tor2.add(40); 
  
        // Displaying the Vector 
        System.out.println("The Vector is: " + vec_tor2); 
  
        System.out.println("Are both of them equal? "
                           + vec_tor1.equals(vec_tor2)); 
    } 
}
输出:
The Vector is: [10, 15, 20, 25, 30]
The Vector is: [10, 15, 20, 25, 30, 40]
Are both of them equal? false


相关用法


注:本文由纯净天空筛选整理自kundankumarjha大神的英文原创作品 Vector equals() Method in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。