先进先出的队列怎么实现??

nbm0 2002-11-25 09:13:21


我需要用一个先进先出的队列
来对一个类对象进行操作
java中有这样的api吗?
没有,那怎么实现?
...全文
1117 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
nbm0 2002-11-26
  • 打赏
  • 举报
回复

看了一下thinking in java,全有!

^ ^
-
nbm0 2002-11-26
  • 打赏
  • 举报
回复


大家太厉害了
看来队列这个东西好像就我不知道
惨…!
谢谢
ilka 2002-11-25
  • 打赏
  • 举报
回复
刚才贴错了
上一个是堆栈
这个是队列

//: c09:Queue.java
// From 'Thinking in Java, 2nd ed.' by Bruce Eckel
// www.BruceEckel.com. See copyright notice in CopyRight.txt.
// Making a queue from a LinkedList.
import java.util.*;

public class Queue {
private LinkedList list = new LinkedList();
public void put(Object v) { list.addFirst(v); }
public Object get() {
return list.removeLast();
}
public boolean isEmpty() {
return list.isEmpty();
}
public static void main(String[] args) {
Queue queue = new Queue();
for(int i = 0; i < 10; i++)
queue.put(Integer.toString(i));
while(!queue.isEmpty())
System.out.println(queue.get());
}
} ///:~
ilka 2002-11-25
  • 打赏
  • 举报
回复
//: c09:StackL.java
// From 'Thinking in Java, 2nd ed.' by Bruce Eckel
// www.BruceEckel.com. See copyright notice in CopyRight.txt.
// Making a stack from a LinkedList.
import java.util.*;
import com.bruceeckel.util.*;

public class StackL {
private LinkedList list = new LinkedList();
public void push(Object v) {
list.addFirst(v);
}
public Object top() { return list.getFirst(); }
public Object pop() {
return list.removeFirst();
}
public static void main(String[] args) {
StackL stack = new StackL();
for(int i = 0; i < 10; i++)
stack.push(Collections2.countries.next());
System.out.println(stack.top());
System.out.println(stack.top());
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
}
} ///:~

我的这个最好
不要从Vector继承
Vector超烂
xue_sharp 2002-11-25
  • 打赏
  • 举报
回复
jdk中原本是个filo队列,现在我改了一下源码,成为一个fifo的。
import java.util.*;
public
class Stack extends Vector {
public Stack() {
}

public Object push(Object item) {
addElement(item);

return item;
}
public synchronized Object pop() {
Object obj;
int len = size();

obj = peek();
removeElementAt(0);

return obj;
}

public synchronized Object peek() {
int len = size();

if (len == 0)
throw new EmptyStackException();
return elementAt(0);
}

public boolean empty() {
return size() == 0;
}
public synchronized int search(Object o) {
int i = lastIndexOf(o);

if (i >= 0) {
return size() - i;
}
return -1;
}

private static final long serialVersionUID = 1224463164541339165L;
}
hello_wyq 2002-11-25
  • 打赏
  • 举报
回复
自己做一个也不是什么困难的事情。
junnef 2002-11-25
  • 打赏
  • 举报
回复
可以象在c++中那样,定义一个类来实现。
疯癫行者 2002-11-25
  • 打赏
  • 举报
回复
使用List吧,从一边加入,另一边取出。

List是java提供的一个接口,有好几个类都能实现该接口,如:ArrayList、LinkedList等,他们的内部实现方法不太一样,进行某些特定操作时有些差别,但各有千秋,你可以根据你的实际需要选择。

具体看一看JavaDoc。

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧