我需要用一个先进先出的队列
来对一个类对象进行操作
java中有这样的api吗?
没有,那怎么实现?
急
8 个解决方案
#1
使用List吧,从一边加入,另一边取出。
List是java提供的一个接口,有好几个类都能实现该接口,如:ArrayList、LinkedList等,他们的内部实现方法不太一样,进行某些特定操作时有些差别,但各有千秋,你可以根据你的实际需要选择。
具体看一看JavaDoc。
List是java提供的一个接口,有好几个类都能实现该接口,如:ArrayList、LinkedList等,他们的内部实现方法不太一样,进行某些特定操作时有些差别,但各有千秋,你可以根据你的实际需要选择。
具体看一看JavaDoc。
#2
可以象在c++中那样,定义一个类来实现。
#3
自己做一个也不是什么困难的事情。
#4
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;
}
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;
}
#5
//: 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超烂
// 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超烂
#6
刚才贴错了
上一个是堆栈
这个是队列
//: 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());
}
} ///:~
上一个是堆栈
这个是队列
//: 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());
}
} ///:~
#7
大家太厉害了
看来队列这个东西好像就我不知道
惨…!
谢谢
#8
看了一下thinking in java,全有!
^ ^
-
#1
使用List吧,从一边加入,另一边取出。
List是java提供的一个接口,有好几个类都能实现该接口,如:ArrayList、LinkedList等,他们的内部实现方法不太一样,进行某些特定操作时有些差别,但各有千秋,你可以根据你的实际需要选择。
具体看一看JavaDoc。
List是java提供的一个接口,有好几个类都能实现该接口,如:ArrayList、LinkedList等,他们的内部实现方法不太一样,进行某些特定操作时有些差别,但各有千秋,你可以根据你的实际需要选择。
具体看一看JavaDoc。
#2
可以象在c++中那样,定义一个类来实现。
#3
自己做一个也不是什么困难的事情。
#4
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;
}
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;
}
#5
//: 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超烂
// 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超烂
#6
刚才贴错了
上一个是堆栈
这个是队列
//: 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());
}
} ///:~
上一个是堆栈
这个是队列
//: 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());
}
} ///:~
#7
大家太厉害了
看来队列这个东西好像就我不知道
惨…!
谢谢
#8
看了一下thinking in java,全有!
^ ^
-