爪哇。util。堆栈push(E element)方法用于将元素推入堆栈。元素被推到堆栈的顶部。
null
语法:
STACK.push(E element)
参数: 该方法接受一个参数 要素 类型为Stack,指要推入堆栈的元素。
返回值: 该方法返回传递的参数。
下面的程序演示了Java。util。堆栈push()方法:
项目1: 将字符串元素添加到堆栈中。
// Java code to illustrate push() method import java.util.*; public class StackDemo { public static void main(String args[]) { // Creating an empty Stack Stack<String> STACK = new Stack<String>(); // Use push() to add elements into the Stack STACK.push( "Welcome" ); STACK.push( "To" ); STACK.push( "Geeks" ); STACK.push( "For" ); STACK.push( "Geeks" ); // Displaying the Stack System.out.println( "Initial Stack: " + STACK); // Pushing elements into the stack STACK.push( "Hello" ); STACK.push( "World" ); // Displaying the final Stack System.out.println( "Final Stack: " + STACK); } } |
输出:
Initial Stack: [Welcome, To, Geeks, For, Geeks] Final Stack: [Welcome, To, Geeks, For, Geeks, Hello, World]
项目2: 将整数元素添加到堆栈中。
// Java code to illustrate push() method import java.util.*; public class StackDemo { public static void main(String args[]) { // Creating an empty Stack Stack<Integer> STACK = new Stack<Integer>(); // Use push() to add elements into the Stack STACK.push( 10 ); STACK.push( 15 ); STACK.push( 30 ); STACK.push( 20 ); STACK.push( 5 ); // Displaying the Stack System.out.println( "Initial Stack: " + STACK); // Pushing elements into the Stack STACK.push( 1254 ); STACK.push( 4521 ); // Displaying the final Stack System.out.println( "Final Stack: " + STACK); } } |
输出:
Initial Stack: [10, 15, 30, 20, 5] Final Stack: [10, 15, 30, 20, 5, 1254, 4521]
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END