改成了泛型的public class CircleQueue<E> { private int head = 0;
private int end = 0;
private int lenth = 5;
private int cur = 0;
private E[] arr;
public CircleQueue() {
arr = (E[]) new Object[10];
} public CircleQueue(int size) {
arr = (E[]) new Object[size];
}
public void print() {
for (int i = 0; i < lenth; i++) {
System.out.print(arr[i] + ",");
}
System.out.println();
} public void add(E ele) throws Exception { if (cur >= 5) {
throw new Exception("full");
}
else {
arr[end] = ele;
cur++;
end++;
if (end >= lenth) {
end = 0;
}
}
print();
} public E poll() throws Exception { if (cur == 0) {
throw new Exception("empty");
} E ret = arr[head];
head++;
if (head >= lenth) {
head = 0;
}
cur--;
print(); return ret;
} public E peek() {
E ret = arr[head];
return ret;
}}
private int end = 0;
private int lenth = 5;
private int cur = 0;
private E[] arr;
public CircleQueue() {
arr = (E[]) new Object[10];
} public CircleQueue(int size) {
arr = (E[]) new Object[size];
}
public void print() {
for (int i = 0; i < lenth; i++) {
System.out.print(arr[i] + ",");
}
System.out.println();
} public void add(E ele) throws Exception { if (cur >= 5) {
throw new Exception("full");
}
else {
arr[end] = ele;
cur++;
end++;
if (end >= lenth) {
end = 0;
}
}
print();
} public E poll() throws Exception { if (cur == 0) {
throw new Exception("empty");
} E ret = arr[head];
head++;
if (head >= lenth) {
head = 0;
}
cur--;
print(); return ret;
} public E peek() {
E ret = arr[head];
return ret;
}}
package com.tur.iterator;import java.util.Arrays;
import java.util.ConcurrentModificationException;
import java.util.Iterator;
import java.util.NoSuchElementException;/**
* 循环队列。
* 在有的场景下非常有用,不停的接首传感器传来的数据,显示到界面上。
* 开辟一个容量为200的循环队列,用于保存新收到的200个传感器数据,
* 新接收到的数据覆盖前面的数据,需要显示的是最近新收到的数据,
* 比这200个还早的数据没有必要继续保存。
* 显示的GUI类遍历队列的数据绘制到界面上。
*
* ArrayBlockingQueue: 用循环数组实现的Queue
*
* for each 循环可以和任何实现了Iterable<E>接口的对象一起工作。
*/
public class CircularQueue<T> implements Iterable<T> {
private int head = 0;
private int size = 0;
private int capacity = 0;
private int modifiedCount = 0;
private T[] data = null; /**
* 创建能够容纳16个元素的循环队列。
*/
public CircularQueue() {
this(16);
} /**
* 创建能够容纳capacity个元素的循环队列。
* 如果capacity小于等于0,则capacity取1。
* @param capacity 队列的容量。
*/
public CircularQueue(int capacity) {
capacity = capacity > 0 ? capacity : 1;
this.capacity = capacity;
data = (T[]) new Object[capacity];
} /**
* 在队尾加入一个元素。
* 如果当前队列的元素个数为其capacity,则新加入的元素放在原来的队首,队列的head后移动一个位置。
* @param element 要加入对尾的元素。
*/
public void enqueue(T element) {
int index = calculateIndex(size);
data[index] = element;
size++;
modifiedCount++; if (size > capacity) {
size = capacity;
head = calculateIndex(1);
}
} /**
* 删除队首元素。
* 但没有元素可删除时抛出一个运行时异常。
* @return 队首的元素。
*/
public T dequeue() {
if (size == 0) {
throw new NoSuchElementException("There is no element");
} T r = data[head];
head = calculateIndex(1);
size--;
modifiedCount++; return r;
} /**
* 取得队首元素。
* 如果队列是空的抛出一个运行时异常。
* @return 队首元素。
*/
public T peek() {
if (size == 0) {
throw new NoSuchElementException("There is no element");
} return data[head];
} /**
* 取得队列中元素的个数。
* @return 队列中元素的个数。
*/
public int size() {
return size;
} /**
* 返回一个Iterator用来顺序遍历队列。
* @return iterator.
*/
public Iterator<T> iterator() {
return new Iterator<T>() {
private int offset = 0;
private int modCount = modifiedCount; @Override
public boolean hasNext() {
return offset < size;
} @Override
public T next() {
// 在遍历队列的时候如果队列被修改了则抛出异常。
if (modCount != modifiedCount) {
throw new ConcurrentModificationException("The queue is modified when iteration.");
} return data[calculateIndex(offset++)];
} @Override
public void remove() {
throw new UnsupportedOperationException("remove() is unsupported.");
}
};
} @Override
public String toString() {
StringBuilder sb = new StringBuilder(); sb.append("[");// Iterator<T> iter = iterator();
// while (iter.hasNext()) {
// sb.append(iter.next() + ", ");
// } for (T elem : this) {
sb.append(elem + ", ");
} sb.append("]"); return String.format("Logical: %s <-----> Physical: %s", sb.toString().replaceAll(", ]$", "]"), Arrays.toString(data));
} /**
* 取得index在队列里真实的下标。
* @param offset
* @return 传入的index在队列里对应的下标。
*/
private int calculateIndex(int offset) {
return (head + offset) % capacity;
}
}
赞!学到了Iterator。