キュー、およびループバッファ!
1475 ワード
//
public class Queue{
public Object[] array;
public int first,next;
public static void main(String args[]){
Queue qu=new Queue();
/*
qu.first=0;
qu.next=5;
*/
qu.first=2;
qu.next=1;
int a=5;
qu.add(a);
String b="abc";
qu.add(b);
System.out.println(qu.next);
while(!qu.isEmpty()){
System.out.println(qu.first+" "+qu.remove());
}
}
public Queue(){
array=new Object[6];
first=0;
next=0;
}
public boolean isEmpty(){
return first==next;
}
public void add(Object item){
//
if((next+1)%array.length==first)
{ /*
、next first ,array ,first,next
、next first ,array ,first ,next
*/
if(next<first)
next=(first+(array.length-1));
//
Object[] newArray=new Object[array.length*2];
array=newArray;
}
array[next]=item;
next=(next+1)%array.length;
}
public Object remove(){
//
if(isEmpty()){
return null;
}else{
Object result=array[first];
//first++;
first=(first+1)%array.length;
return result;
}
}
}
JDK 1のようです.4の