简体   繁体   中英

How does the pop() functionality work in stacks in Java?

I have searched online for the code implementation of the pop() function in Java. I frequently see that the counter(where the top of the stack is) is reduced by 1, effectively making it inaccessible but not completely deleting it until it is overwritten. Is that how it is implemented in the language as well, or this that due to the fact that they use arrays to implement a stack functionality? Also, does the language use arrays, linked lists, or some other data structure to implement stacks? Thanks!

For the record, the question has been changed since I wrote the answer below. The question originally talked about queues and stacks. Now the title asks only about queues, while the body of the question talks only of stacks.


The title to the question mentions both queue and stack, but these are not the same thing. A queue is a structure that, in the common case, implements a first-in-first-out discipline (a deque -- for double-ended queue -- is a different matter). Think of the queue for a bus: the person who first arrives at the bus stop should be the first person to get on the bus when it arrives. By contrast, a stack shows last-in-first-out behavior. The name tends to show that: items are 'stacked' on top of one another, and the one at the top (the last one placed) is the one that is accessible.

queue is an interface in Java, not a class, so there is more than one implementation. The version 7 JDK lists 13 known implementing classes. As long as pop fulfills the semantics of removing the front (oldest) element, it is doing the job correctly. However pop is an inappropriate name for a method on a queue, and in fact the java.util.queue interface does not define pop .

Unlike queue , stack is a class rather than an interface in Java, and is based on the older vector class. I imagine, therefore, that the last-inserted item has the highest index in the vector, and pop just removes that. Something like

    E pop() { remove(size()-1); }

or at least a logically-equivalent sequence. That's the obvious implementation based on a vector. As pointed out elsewhere, it's useful if the implementation also nulls out the now-inaccessible cell in the vector, so that it does not retain a reference to the object which is no longer "in" the stack.

https://docs.oracle.com/javase/7/docs/api/java/util/Stack.html

public E pop() Removes the object at the top of this stack and returns that object as the value of this function.

All Implemented Interfaces: Serializable, Cloneable, Iterable, Collection, List, RandomAccess

A stack is effectively a Vector, which is a List, so your assumption about its deeper functionality is correct.

Source code for stack can be found here: http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/util/Stack.java

It calls removeElement from the parent class, Vector. There in the javadoc you should also note that they recommend that you use Deque instead, which is a double ended queue. Vector is backed by an array and it does set the element in the array to null when it is popped so that the garbage collector can do its work when the object isn't needed anymore.

Vector source code: http://hg.openjdk.java.net/jdk7/jdk7/jdk/file/00cd9dc3c2b5/src/share/classes/java/util/Vector.java

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM