![]() As soon as one element gets popped from stack top of the stack will decrease its value by one to point to the next latest element into the stack. This operation on the stack is called pop operation. Once we are having an element in the stack, We can remove elements from the stack. This is how the top of the stack always points to the latest element pushed onto the stack. As soon as a new element book 2 gets pushed into stack top again gets incremented by one and start pointing to book 2. When we pushed book one into stack top of the stack gets incremented by one and start pointing to book 1. Here is an example of pushing an element (object) onto a Java Stack: Stack stackOfBooks = new Stack() Įxplanation: In the above example we have created a java Stack and then used it push() to add elements (book 1, book 2) into it. You can use push() method to add elements into a Java Stack. Thus, you actually push objects to the Stack. The elements you push onto the Stack must be Java objects. Once you have a Java Stack instance, you can push elements to the top of the Stack. Here is an example of creating a Java Stack instance: Stack stackOfBooks = new Stack() To use a Java Stack you must first create an instance of the Stack class. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |