push() Method

To add an element to the top of the stack, we use the push() method. For example,


\begin{lstlisting}
import java.util.Stack;
\par
class Main {
public static void...
...(''Cat'');
\par
System.out.println(''Stack: '' + animals);
}
}
\end{lstlisting}

Output


\begin{lstlisting}
Stack: [Dog, Horse, Cat]
\end{lstlisting}