- Java Data Structures Resources
- Java Data Structures - Quick Guide
- Java Data Structures - Resources
- Java Data Structures - Discussion
Verifying if a Stack is empty
The Stack class of the java.util package provides a isEmpty() method. This method verifies whether the current Stack is empty or not. If the given vector is empty this method returns true else it returns false.
Example
import java.util.Stack;
public class StackIsEmpty {
public static void main(String args[]) {
Stack stack = new Stack();
stack.push(455);
stack.push(555);
stack.push(655);
stack.push(755);
stack.push(855);
stack.push(955);
System.out.println("Contents of the stack :"+stack);
stack.clear();
System.out.println("Contents of the stack after clearing the elements :"+stack);
if(stack.isEmpty()) {
System.out.println("Given stack is empty");
} else {
System.out.println("Given stack is not empty");
}
}
}
Output
Contents of the stack :[455, 555, 655, 755, 855, 955] Contents of the stack after clearing the elements :[] Given stack is empty
Advertisements