Java 集合(LIFO 结构) [英] Java Collections (LIFO Structure)
问题描述
我正在 Java 的集合框架中寻找 LIFO 结构(堆栈),但没有成功.基本上我想要一个非常简单的堆栈;我的最佳选择是 Deque,但我使用的是 Java 1.5.
I am looking in the Collections framework of Java for a LIFO Structure (Stack) without any success. Basically I want a really simple stack; my perfect option would be a Deque, but I am in Java 1.5.
我不想在我的结构中添加另一个类,但我想知道这是否可行:
I would like not to have to add another class to my structure but I am wondering if that is possible:
集合框架 (1.5) 中是否有任何类可以完成这项工作?
Is there any class in the Collections framework (1.5) that does the job?
如果没有,有没有办法在不重新实现的情况下在 LIFO 队列(又名堆栈)中转换队列?
If not, is there any way to turn a Queue in a LIFO Queue (aka Stack) without reimplementation?
如果不是,我应该为这个任务扩展哪个接口或类?我想保持 Sun 公司的 Deque 的方式是一个好的开始.
If not, which Interface or class should I extend for this task? I guess that keep the way that the guys of Sun have made with the Deque is a good start.
非常感谢.
忘记说Stack类了:当我看到它实现了Vector类时,我对这个类产生了怀疑,而且Vector类有点过时了,不是吗?
I forgot to say about the Stack class: I have my doubts about this class when I saw that it implements the Vector class, and the Vector class is a little bit obsolete, isn't it?
推荐答案
实际上有一个 Stack 类:http://java.sun.com/j2se/1.5.0/docs/api/java/util/Stack.html
There's actually a Stack class: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Stack.html
如果您不想使用它,LinkedList 类 (http://java.sun.com/j2se/1.5.0/docs/api/java/util/LinkedList.html) 有 addFirst
和 addLast
和 removeFirst
和 removeLast
方法,使其非常适合用作堆栈或队列类.
If you don't want to use that, the LinkedList class (http://java.sun.com/j2se/1.5.0/docs/api/java/util/LinkedList.html) has addFirst
and addLast
and removeFirst
and removeLast
methods, making it perfect for use as a stack or queue class.
这篇关于Java 集合(LIFO 结构)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!