简体   繁体   English

pop() 功能如何在 Java 的堆栈中工作?

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

I have searched online for the code implementation of the pop() function in Java.我在网上搜索了Java中pop()函数的代码实现。 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.我经常看到计数器(堆栈顶部所在的位置)减 1,有效地使其无法访问,但在被覆盖之前不会完全删除它。 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. queue是 Java 中的一个接口,而不是一个类,所以有不止一个实现。 The version 7 JDK lists 13 known implementing classes.版本 7 JDK 列出了 13 个已知的实现类。 As long as pop fulfills the semantics of removing the front (oldest) element, it is doing the job correctly.只要pop满足删除前面(最旧)元素的语义,它就可以正确地完成工作。 However pop is an inappropriate name for a method on a queue, and in fact the java.util.queue interface does not define pop .然而,对于队列上的方法来说, pop是一个不合适的名称,实际上java.util.queue接口并没有定义pop

Unlike queue , stack is a class rather than an interface in Java, and is based on the older vector class.queue不同, stack是一个类而不是 Java 中的接口,并且基于旧的vector类。 I imagine, therefore, that the last-inserted item has the highest index in the vector, and pop just removes that.因此,我想最后插入的项目在向量中具有最高的索引,而pop只是将其删除。 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 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. public E pop() 移除此堆栈顶部的对象并将该对象作为此函数的值返回。

All Implemented Interfaces: Serializable, Cloneable, Iterable, Collection, List, RandomAccess所有实现的接口: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堆栈的源代码可以在这里找到: 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.它从父类 Vector 调用 removeElement。 There in the javadoc you should also note that they recommend that you use Deque instead, which is a double ended queue.在 javadoc 中,您还应该注意他们建议您改用 Deque,这是一个双端队列。 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 由一个数组支持,当它被弹出时,它确实将数组中的元素设置为 null,以便垃圾收集器可以在不再需要该对象时执行其工作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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