基于ArrayList实现的简易版ArrayQueue
Posted esrevinud
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于ArrayList实现的简易版ArrayQueue相关的知识,希望对你有一定的参考价值。
参考接口
public interface myQueue<T> {
void offer(T element);
T poll();
T peek();
Boolean isEmpty();
Integer size();
void clear();
}
实现代码
import java.util.ArrayList;
public class MyArrayQueue<T> implements myQueue<T> {
private ArrayList<T> core;
public MyArrayQueue() {
this(10);
}
public MyArrayQueue(int capacity) {
core = new ArrayList<>(capacity);
}
@Override
public void offer(T element) {
core.add(element);
}
@Override
public T poll() {
return core.remove(0);
}
@Override
public T peek() {
return core.get(0);
}
@Override
public Boolean isEmpty() {
return core.isEmpty();
}
@Override
public Integer size() {
return core.size();
}
@Override
public void clear() {
core.clear();
}
public static void main(String[] args) {
MyArrayQueue<Integer> queue = new MyArrayQueue<>();
for (int i = 0; i < 10; i ++) {
queue.offer(i);
}
System.out.println(queue.peek());
queue.poll();
System.out.println(queue.peek());
}
}
以上是关于基于ArrayList实现的简易版ArrayQueue的主要内容,如果未能解决你的问题,请参考以下文章