Java Vector equals()方法
Java中Vector类的java.util.vector .equals(Object obj)方法用于验证一个对象与一个向量的平等性,并对它们进行比较。只有当两个向量都包含相同的元素且顺序相同时,该列表才会返回真。
语法
first_vector.equals(second_vector)
参数: 该方法接受一个强制性参数second_vector,它指的是第二个向量与第一个向量的比较。
返回值: 如果相等成立,并且对象和向量都相等,该方法返回真,否则返回假。
以下程序用于说明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
极客教程