队列满,则进行♻️循环处理。
/**
* 先进先出
*
* @author luobo.cs@raycloud.com
* @since 2021/7/25 10:47 上午
*/
public class ArrayListQueue<E> {
private final Object[] elements;
private int tail;
private int head;
private int size = 0;
private final int initialCapacity;
public ArrayListQueue(int initialCapacity) {
this.initialCapacity = initialCapacity;
this.elements = new Object[this.initialCapacity];
tail = 0;
head = 0;
}
public int size() {
return size;
}
/**
* 添加
*/
public void push(E e) {
if (e == null) {
throw new NullPointerException("element can't be null");
}
if (size >= initialCapacity) {
throw new ArrayIndexOutOfBoundsException("queue is full");
}
elements[tail] = e;
tail++;
size++;
if (tail >= initialCapacity) {
tail = 0;
}
if (size >= initialCapacity) {
size = initialCapacity;
}
}
@SuppressWarnings("unchecked")
public E poll() {
final Object element = elements[head];
if (element == null) {
return null;
}
elements[head] = null;
head++;
size--;
if (head >= initialCapacity) {
head = 0;
}
if (size <= 0) {
size = 0;
}
return (E) element;
}
public static void main(String[] args) {
ArrayListQueue<String> queue = new ArrayListQueue<>(3);
queue.push("11");
queue.push("22");
queue.push("33");
System.out.println(queue.poll());
System.out.println(queue.poll());
System.out.println(queue.poll());
System.out.println(queue.poll());
queue.push("44");
queue.push("55");
queue.push("66");
System.out.println(queue.poll());
System.out.println(queue.poll());
System.out.println(queue.poll());
}
}