📜  Java中的堆栈pop()方法

📅  最后修改于: 2022-05-13 01:55:38.698000             🧑  作者: Mango

Java中的堆栈pop()方法

Java中的Java .util.Stack.pop() 方法用于从堆栈中弹出一个元素。元素从栈顶弹出并从栈顶移除。
句法:

STACK.pop()

参数:该方法不带任何参数。
返回值:此方法返回存在于堆栈顶部的元素,然后将其删除。
异常:如果堆栈为空,则抛出EmptyStackException方法。
下面的程序说明了Java.util.Stack.pop() 方法:
方案一:

Java
// Java code to illustrate pop()
import java.util.*;
 
public class StackDemo {
    public static void main(String args[])
    {
        // Creating an empty Stack
        Stack STACK = new Stack();
 
        // Use add() method to add elements
        STACK.push("Welcome");
        STACK.push("To");
        STACK.push("Geeks");
        STACK.push("For");
        STACK.push("Geeks");
 
        // Displaying the Stack
        System.out.println("Initial Stack: " + STACK);
 
        // Removing elements using pop() method
        System.out.println("Popped element: " +
                                         STACK.pop());
        System.out.println("Popped element: " +
                                         STACK.pop());
 
        // Displaying the Stack after pop operation
        System.out.println("Stack after pop operation "
                                             + STACK);
    }
}


Java
// Java code to illustrate pop()
import java.util.*;
 
public class StackDemo {
    public static void main(String args[])
    {
        // Creating an empty Stack
        Stack STACK = new Stack();
 
        // Use add() method to add elements
        STACK.push(10);
        STACK.push(15);
        STACK.push(30);
        STACK.push(20);
        STACK.push(5);
 
        // Displaying the Stack
        System.out.println("Initial Stack: " + STACK);
 
        // Removing elements using pop() method
        System.out.println("Popped element: " +
                                         STACK.pop());
        System.out.println("Popped element: " +
                                         STACK.pop());
 
        // Displaying the Stack after pop operation
        System.out.println("Stack after pop operation "
                                             + STACK);
    }
}


方案二:

Java

// Java code to illustrate pop()
import java.util.*;
 
public class StackDemo {
    public static void main(String args[])
    {
        // Creating an empty Stack
        Stack STACK = new Stack();
 
        // Use add() method to add elements
        STACK.push(10);
        STACK.push(15);
        STACK.push(30);
        STACK.push(20);
        STACK.push(5);
 
        // Displaying the Stack
        System.out.println("Initial Stack: " + STACK);
 
        // Removing elements using pop() method
        System.out.println("Popped element: " +
                                         STACK.pop());
        System.out.println("Popped element: " +
                                         STACK.pop());
 
        // Displaying the Stack after pop operation
        System.out.println("Stack after pop operation "
                                             + STACK);
    }
}
输出:
Initial Stack: [10, 15, 30, 20, 5]
Popped element: 5
Popped element: 20
Stack after pop operation [10, 15, 30]