Java中的Stack類的Java.util.Stack.equals(Object obj)方法用於驗證Object與Stack的相等性並進行比較。僅當兩個Stack包含相同順序的相同元素時,列表才返回true。
用法:
first_Stack.equals(second_Stack)
參數:此方法接受強製性參數second_Stack,該參數表示第二個要與第一個堆棧進行比較的堆棧。
返回值:如果相等相等且對象和Stack相等,則該方法返回true,否則返回false。
以下程序用於說明java.util.Stack.elements()方法的用法:
示例1:
// Java code to illustrate the equals() method
import java.util.*;
public class Stack_Demo {
public static void main(String[] args)
{
// Creating an empty Stack
Stack<String> stack1 = new Stack<String>();
// Inserting elements into the table
stack1.add("Geeks");
stack1.add("4");
stack1.add("Geeks");
stack1.add("Welcomes");
stack1.add("You");
// Displaying the Stack
System.out.println("The Stack is: "
+ stack1);
// Creating an empty Stack
Stack<String> stack2 = new Stack<String>();
// Inserting elements into the table
stack2.add("Geeks");
stack2.add("4");
stack2.add("Geeks");
stack2.add("Welcomes");
stack2.add("You");
// Displaying the Stack
System.out.println("The Stack is: "
+ stack2);
System.out.println("Are both of them equal? "
+ stack1.equals(stack2));
}
}
輸出:
The Stack is: [Geeks, 4, Geeks, Welcomes, You] The Stack 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 Stack_Demo {
public static void main(String[] args)
{
// Creating an empty Stack
Stack<Integer> stack1 = new Stack<Integer>();
// Inserting elements into the table
stack1.add(10);
stack1.add(15);
stack1.add(20);
stack1.add(25);
stack1.add(30);
// Displaying the Stack
System.out.println("The Stack is: " + stack1);
// Creating an empty Stack
Stack<Integer> stack2 = new Stack<Integer>();
// Inserting elements into the table
stack2.add(10);
stack2.add(15);
stack2.add(20);
stack2.add(25);
stack2.add(30);
stack2.add(40);
// Displaying the Stack
System.out.println("The Stack is: " + stack2);
System.out.println("Are both of them equal? "
+ stack1.equals(stack2));
}
}
輸出:
The Stack is: [10, 15, 20, 25, 30] The Stack is: [10, 15, 20, 25, 30, 40] Are both of them equal? false
相關用法
- Java Map equals()用法及代碼示例
- Java AbstractSequentialList equals()用法及代碼示例
- Java IdentityHashMap equals()用法及代碼示例
- Java MessageFormat equals()用法及代碼示例
- Java GregorianCalendar equals()用法及代碼示例
- Java HashSet equals()用法及代碼示例
- Java LinkedBlockingDeque equals()用法及代碼示例
- Java LongBuffer equals()用法及代碼示例
- Java CharBuffer equals()用法及代碼示例
- Java StringWriter equals()用法及代碼示例
- Java FieldPosition equals()用法及代碼示例
- Java Set equals()用法及代碼示例
- Java BigDecimal equals()用法及代碼示例
- Java ConcurrentLinkedDeque equals()用法及代碼示例
注:本文由純淨天空篩選整理自Code_r大神的英文原創作品 Stack equals() method in Java with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。