import java.util.Random; public class Queue { private int[] queue; //计数,当前队列中的数据个数 public static int count = 0; //当每次超出范围时,数组长度自动增加10 public static final int MAX = 10;
public Queue() { this(10); }
public Queue(int count) { if(count < 10) { count = 10; } queue = new int[count]; }
public static void main(String[] args) { Queue myQueue = new Queue(); //加入10个随机值 Random random = new Random(); for(int i=0; i<10; i++) { myQueue.add(random.nextInt(100)); }
private Object[] os;
private int head;
private int tail;
public MyQueue(){
this(8);
}
public MyQueue(int count){
if(count<8){
count=8;
}
os=new Object[count];
}
/**
* 入队
* @param e
*/
public void add(E e){
if(tail>=os.length){
if(head>0){
System.arraycopy(os,head,os,0,tail-head);
tail-=head;
head=0;
}
else{
os=Arrays.copyOf(os,os.length*2);
}
}
os[tail++]=e;
}
/**
* 出队
* @return
*/
public E remove(){
if(head>=tail){
throw new EmptyQueueException();
}
E r=(E)os[head];
os[head++]=null;
if(head>=tail){
head=tail=0;
}
return r;
}
public static class EmptyQueueException extends RuntimeException{
}
public static void main(String[] args){
MyQueue<String> q=new MyQueue<String>();
for(int i=0;i<100;i++){
q.add(""+i);
}
for(int i=0;i<100;i++){
System.out.println(q.remove());
}
for(int i=0;i<100;i++){
q.add(""+i);
System.out.println(q.remove());
}
}
}
import java.util.Random;
public class Queue {
private int[] queue;
//计数,当前队列中的数据个数
public static int count = 0;
//当每次超出范围时,数组长度自动增加10
public static final int MAX = 10;
public Queue() {
this(10);
}
public Queue(int count) {
if(count < 10) {
count = 10;
}
queue = new int[count];
}
/**
* 入队,当长度超出当前队列的最大长度后,长度自动增加MAX
* @param num 入队的数据
*/
public void add(int num) {
if(count >= queue.length) {
int[] temp = new int[queue.length + MAX];
for(int i=0; i<queue.length; i++) {
temp[i] = queue[i];
}
queue = temp;
}
queue[count ++] = num;
}
/**
* 出队,当队列中没有元素时,打印信息,并退出程序。
* @return 删除的元素
*/
public int remove() {
if(count <= 0) {
System.out.println("当前队列中没有任何数据!");
System.exit(0);
}
int temp = queue[0];
for(int i=0; i<queue.length - 1; i++) {
queue[i] = queue[i + 1];
}
count --;
return temp;
}
/**
* 覆盖父类的toString方法,返回该队列中的所有元素
*/
@Override
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("[");
for(int i=0; i<count; i++) {
sb.append(queue[i]);
if(i != count - 1) {
sb.append(",");
}
}
sb.append("]");
return sb.toString();
}
public static void main(String[] args) {
Queue myQueue = new Queue();
//加入10个随机值
Random random = new Random();
for(int i=0; i<10; i++) {
myQueue.add(random.nextInt(100));
}
//打印出当前队列中的数据
System.out.println("队列中原有的值: " + myQueue);
//加入1个数据,并打印
myQueue.add(110);
System.out.println("加入数据后队列中的值: " + myQueue);
//打印移除的数据,并打印移除后的队列
System.out.println("移除的数据为:" + myQueue.remove());
System.out.println("移除后队列中的值:" + myQueue);
}
}
在末尾添加:List <Object> li=new List<>();
........
li.Add(object);
li.TrimToSize();
移除第一个:li.RemonveAt(0);
ArrayList.add()
ArrayList.remove()两个方法就可以满足你的要求了··
* 队列FIFO的接口
*
* @author 鼎鼎
*
* @param <E>
*/
public interface Queue<E> {
/**
* 入队: 从队尾加入一元素
*
* @param target
*/
public void add(E target);
/**
* 出队: 移走队头元素
*
* @param target
* @return 当前队头元素
*/
public E remove();
/**
* 当前队列中的元素个数
*/
public int size();
/**
* 判断当前队列是否为空
*
* @return
*/
public boolean isEmpty();
/**
* 只是返回队头元素
* @return
*/
public E front();
}
private E[] data;
// 当前队列中元素的大小
private int size;
private int front;
private int rear;
public ArrayQueue() {
data = (E[]) new Object[20];
size = 0;
front = 0;
rear = 0;
}
public void add(E target) {
if (isFull()) {
enlarge();
//数组队列满后,需要扩充,记住扩充后要将front的值归0
front=0;
}
rear = (front + size) % data.length;
data[rear] = target;
size++;
}
public boolean isEmpty() {
return size == 0;
}
/**
* 判断当前队列是否已满
*
* @return
*/
public boolean isFull() {
return size == data.length;
}
/**
* 将数组容量扩大两倍
*
*/
public void enlarge() {
E[] newData = (E[]) new Object[data.length * 2];
for (int i = 0; i < data.length; i++) {
newData[i] = data[i];
}
data = newData;
newData = null;
}
public E remove() {
if (isEmpty()) {
throw new RuntimeException("队列为空!");
}
E tempData = data[front];
data[front] = null;
front = (front + 1) % (data.length);
size--;
return tempData;
}
public int size() {
return size;
}
public E front() {
if (isEmpty()) {
throw new RuntimeException("队列为空!");
}
return data[front];
}
}