當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。