Метод Stack push () в Java
Метод Java.util.Stack.push (элемент E) используется для помещения элемента в стек. Элемент помещается на вершину стека.
Синтаксис:
STACK.push ( элемент E )
Параметры: метод принимает один элемент параметра типа Stack и относится к элементу, который должен быть помещен в стек.
Возвращаемое значение: метод возвращает переданный аргумент.
Ниже программы иллюстрируют метод Java.util.Stack.push ():
Program 1: Adding String elements into the Stack.
// 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]
Program 2: Adding Integer elements into the Stack.
// 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]
Вниманию читателя! Не переставай учиться сейчас. Ознакомьтесь со всеми важными концепциями Java Foundation и коллекций с помощью курса "Основы Java и Java Collections" по доступной для студентов цене и будьте готовы к работе в отрасли. Чтобы завершить подготовку от изучения языка к DS Algo и многому другому, см. Полный курс подготовки к собеседованию .