Stack push(K) in Java

push(K): This method is available in java.util.Stack class of Java.

Syntax:

K java.util.Stack.push(K item)

This method takes one argument. This method pushes an item onto the top of this stack.

Parameters: One parameter is required for this method.

item: the item to be pushed onto this stack.

Returns: the item argument.

Exceptions: NA

Approach

Java

import java.util.Stack;

public class Stackpush {
    public static void main(String[] args) {

        Stack<Integer> st = new Stack<>();

        st.push(1);
        st.push(18);
        st.push(29);
        st.push(8);

        System.out.println(st);

    }
}

Output:

[1, 18, 29, 8]


No comments:

Post a Comment