Java 使用 Vector 实现 Stack 类,为什么?虽然看起来 Arraylist 应该是更好的选择。 Java 希望 Stack 是线程安全的,或者一般来说 Stack 是强制性的(我不认为是这种情况)是线程安全的,还是有任何其他理论可以使用 Vector 在 Java 中实现 Stack?
最佳答案
Stack
和 Vector
出现在 JDK 1.0 中。 ArrayList
直到 JDK 1.2 才被添加。 Stack
对 Vector
的继承已融入 API,因此现在无法更改。
但无论如何,这是一个过时的类(class)。修复它不是优先事项。 Stack
自己的 Javadoc writeup 说:
A more complete and consistent set of LIFO stack operations is provided by the
Deque
interface and its implementations, which should be used in preference to this class. For example:Deque<Integer> stack = new ArrayDeque<Integer>();
关于java - 为什么 java.util.Stack 是使用 Vector 而不是 Arraylist 实现的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39559872/