本文目录一览:
java 用什么实现 FIFO队列?
java使用数据结构来实现FIFO先进先出的队列,实例如下:
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package linkedlisttest;
import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.List;
/**
*
* @author Vicky.H
* @email eclipser@163.com
*/
public class FIFOTest {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
FIFOA fifo = new FIFOImplA(5);
for (int i = 0; i 20; i++) {
A a = new A("A:" + i);
A head = fifo.addLastSafe(a);
System.out.println(i + "\thead:" + head + "\tsize:" + fifo.size());
}
System.out.println("---------------");
System.out.println("弹出数据");
ListA polls = fifo.setMaxSize(3);
for (A a : polls) {
System.out.println("\thead:" + a);
}
System.out.println("剩余数据");
for (A a : fifo) {
System.out.println("\thead:" + a);
}
System.out.println(fifo.size());
}
}
interface FIFOT extends ListT, DequeT, Cloneable, java.io.Serializable {
/**
* 向最后添加一个新的,如果长度超过允许的最大值,则弹出一个 *
*/
T addLastSafe(T addLast);
/**
* 弹出head,如果Size = 0返回null。而不同于pop抛出异常
* @return
*/
T pollSafe();
/**
* 获得最大保存
*
* @return
*/
int getMaxSize();
/**
* 设置最大存储范围
*
* @return 返回的是,因为改变了队列大小,导致弹出的head
*/
ListT setMaxSize(int maxSize);
}
class FIFOImplT extends LinkedListT implements FIFOT {
private int maxSize = Integer.MAX_VALUE;
private final Object synObj = new Object();
public FIFOImpl() {
super();
}
public FIFOImpl(int maxSize) {
super();
this.maxSize = maxSize;
}
@Override
public T addLastSafe(T addLast) {
synchronized (synObj) {
T head = null;
while (size() = maxSize) {
head = poll();
}
addLast(addLast);
return head;
}
}
@Override
public T pollSafe() {
synchronized (synObj) {
return poll();
}
}
@Override
public ListT setMaxSize(int maxSize) {
ListT list = null;
if (maxSize this.maxSize) {
list = new ArrayListT();
synchronized (synObj) {
while (size() maxSize) {
list.add(poll());
}
}
}
this.maxSize = maxSize;
return list;
}
@Override
public int getMaxSize() {
return this.maxSize;
}
}
class A {
private String name;
public A() {
}
public A(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "A{" + "name=" + name + '}';
}
}
java中怎么实现队列
public class QueueE {
private Object[] data=null;
private int maxSize; //队列容量
private int front; //队列头,允许删除
private int rear; //队列尾,允许插入
//构造函数
public Queue(){
this(10);
}
public Queue(int initialSize){
if(initialSize =0){
this.maxSize = initialSize;
data = new Object[initialSize];
front = rear =0;
}else{
throw new RuntimeException("初始化大小不能小于0:" + initialSize);
}
}
//判空
public boolean empty(){
return rear==front?true:false;
}
//插入
public boolean add(E e){
if(rear== maxSize){
throw new RuntimeException("队列已满,无法插入新的元素!");
}else{
data[rear++]=e;
return true;
}
}
//返回队首元素,但不删除
public E peek(){
if(empty()){
throw new RuntimeException("空队列异常!");
}else{
return (E) data[front];
}
}
//出队
public E poll(){
if(empty()){
throw new RuntimeException("空队列异常!");
}else{
E value = (E) data[front]; //保留队列的front端的元素的值
data[front++] = null; //释放队列的front端的元素
return value;
}
}
//队列长度
public int length(){
return rear-front;
}
}
用java编一个队列
自己写了个简单的实现
class QueueE{
private Object[] integerQueue;//用来当队列
public int tail;//队尾
public int size;//队的长度,也可以设置一个默认值,溢出时从新申请
public Queue(int size){
integerQueue=new Object[size];
this.size=size;
tail=-1;
}
/**
* 将元素插入队列
* @return 如果该元素已添加到此队列,则返回 true;否则返回 false
*/
public boolean offer(E e){
if(tail size-1){
tail++;
this.integerQueue[tail]=e;
return true;
}else{
return false;
}
}
/**
* 获取并移除此队列的头,如果此队列为空,则返回 null。
*/
public E poll(){
Object tmp;
if(tail=0){
tmp=this.integerQueue[tail];
tail--;
return (E)tmp;
}else{
return null;
}
}
}
java中的队列用什么实现
队列的实现单纯的是数据结构的问题,既可以用链表结构实现队列,也可以用数组实现。这和语言不是紧密关系,java可以这样实现,C、C++ 也可以。