For one thing, it's more sensible in terms of inheritance.
(一方面,在继承方面更明智。)
The fact that Stack
extends Vector
is really strange, in my view. (在我看来, Stack
扩展Vector
的事实确实很奇怪。)
Early in Java, inheritance was overused IMO - Properties
being another example. (在Java早期,继承被IMO过度使用- Properties
是另一个示例。)
For me, the crucial word in the docs you quoted is consistent .
(对我而言,您引用的文档中的关键词是一致的 。)
Deque
exposes a set of operations which is all about being able to fetch/add/remove items from the start or end of a collection, iterate etc - and that's it. (Deque
公开了一组操作,这些操作都与从集合的开始或结束取回,添加/删除项,进行迭代等有关,仅此而已。)
There's deliberately no way to access an element by position, which Stack
exposes because it's a subclass of Vector
. (故意没有办法按位置访问元素,这是Stack
公开的, 因为它是Vector
的子类。)
Oh, and also Stack
has no interface, so if you know you need Stack
operations you end up committing to a specific concrete class, which isn't usually a good idea.
(哦,而且Stack
也没有接口,所以如果您知道需要Stack
操作,您最终会提交给特定的具体类,这通常不是一个好主意。)
Also as pointed out in the comments, Stack
and Deque
have reverse iteration orders:
(同样如注释中所指出的, Stack
和Deque
具有反向迭代顺序:)
Stack<Integer> stack = new Stack<>();
stack.push(1);
stack.push(2);
stack.push(3);
System.out.println(new ArrayList<>(stack)); // prints 1, 2, 3
Deque<Integer> deque = new ArrayDeque<>();
deque.push(1);
deque.push(2);
deque.push(3);
System.out.println(new ArrayList<>(deque)); // prints 3, 2, 1
which is also explained in the JavaDocs for Deque.iterator() :
(JavaDocs for Deque.iterator()也对此进行了说明:)
Returns an iterator over the elements in this deque in proper sequence.
(以适当的顺序返回此双端队列中的元素的迭代器。)
The elements will be returned in order from first (head) to last (tail). (元素将按照从头(头)到后(尾)的顺序返回。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…