Java集合(LIFO结构) [英] Java Collections (LIFO Structure)

查看:481
本文介绍了Java集合(LIFO结构)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java的Collections框架中查找LIFO结构(Stack)没有任何成功。基本上我想要一个非常简单的堆栈;我完美的选择是Deque,但我在Java 1.5。



我不想添加另一个类到我的结构,但我想知道如果这是可能:


  1. 在Collections框架(1.5)中有任何类可以完成这项工作吗?


  2. 如果没有,是否有任何方法可以在没有重新实现的情况下转换LIFO队列(aka Stack)中的队列?


  3. 如果不是,我应该为此任务扩展哪个接口或类?我想,保持太阳的家伙用Deque做的方式是一个好的开始。


很多。



编辑:我忘了说Stack类:当我看到它实现了Vector类时,我对这个类有疑问,Vector类是

实际上有一个Stack类: java.sun.com/j2se/1.5.0/docs/api/java/util/Stack.htmlrel =nofollow noreferrer> 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 方法,非常适合用作堆栈或队列类。


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. Is there any class in the Collections framework (1.5) that does the job?

  2. If not, is there any way to turn a Queue in a LIFO Queue (aka Stack) without reimplementation?

  3. 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.

Thanks a lot.

EDIT: 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?

解决方案

There's actually a Stack class: http://java.sun.com/j2se/1.5.0/docs/api/java/util/Stack.html

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆