标签:简易 实现 empty java ring myarray util list ++
参考接口
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());
}
}
标签:简易 实现 empty java ring myarray util list ++
原文地址:https://www.cnblogs.com/esrevinud/p/12150327.html