A stack is a linear table that can only be inserted, fetched, or deleted at the end of the linear table. This linear table has LIFO (LIFO) feature, then how to implement this function in Java, hehe, Java has provided us with the Api--stack,stack class inherits the Vector class. Here's an example to get a look at the API:
Import Java.util.stack;public class Teststack {public static void main (string[] args) {stack<string> Stack = new Sta Ck<string> (); Stack.push ("a");//press the data onto the top of the stack, which acts addelement as stack.addelement ("B") below; Stack.push ("C"); Stack.push ("D"); Stack.push ("E"); if (!stack.empty ()) {//Determines whether the stack is empty System.out.println (Stack.peek ());//Output E. Gets the object at the top of the stack (the last item of the Vector object), but does not remove the object from the stack, and throws a Emptystackexception exception System.out.println (Stack.pop ()) If the stack is empty; Output E. Gets the object at the top of the stack (the last item of the Vector object) and removes the object from the top of the stack and throws a Emptystackexception exception System.out.println (Stack.pop ()) If the stack is empty; Output d. System.out.println (Stack.search ("C"));//Gets the position of the specified object in the stack, with a base of 1, and returns 1}} if no
Stack Api--stack in Java