引用的前面设计的数组类
Array.java
/**
* Array
*
* @author : lao
* @date : 2019/11/2 13:17
*/
public class Array<E> {
private E[] data;
private int size;
/**
* 构造函数
*
* @param capacity 传入数值初始值大小
*/
public Array(int capacity) {
//Java不支持直接构造函数中new 泛型数据类型,使用强制装换
// data = new E[capacity];
data = (E[])new Object[capacity];
size = 0;
}
/**
* 无参构造函数,默认容量capacity=10
*/
public Array() {
this(10);
}
/**
* @return 返回数组元素的个数
*/
public int getSize() {
return size;
}
/**
* @return 返回数组的容量
*/
public int getCapacity() {
return data.length;
}
public boolean isEmpty() {
return size == 0;
}
/**
* 向数组中添加一个元素
*
* @param e
*/
public void addLast(E e) {
add(size, e);
}
/**
* 向数组添加一个 元素
*
* @param e
*/
public void addFirst(E e) {
add(0, e);
}
/**
* 在index位置插入一个元素
*
* @param index 位置
* @param e 参数值
*/
public void add(int index, E e) {
if (index < 0 || index > size) {
throw new IllegalArgumentException("AddList failed.Require index>=0 and index <= size");
}
if (size == data.length) {
resize(2 * data.length);
}
for (int i = size - 1; i >= index; i--) {
data[i + 1] = data[i];
}
data[index] = e;
size++;
}
/**
* 获取数组中某一元素
* @param index
* @return
*/
public E get(int index) {
if (index < 0 || index >= size) {
throw new IllegalArgumentException("Get failed.Index is illegal.");
}
return data[index];
}
public E getLast() {
return get(size - 1);
}
public E getFirst() {
return get(0);
}
/**
* 设置某一位置的元素值
* @param index
* @param e
*/
public void set(int index, E e) {
if (index < 0 || index >= size) {
throw new IllegalArgumentException("Get failed.Index is illegal.");
}
data[index] = e;
}
/**
* 查找数组中是否包含某一元素e
* @return
*/
public boolean contains(E e) {
for (int i = 0; i < size; i++) {
if (data[i].equals(e)) {
return true;
}
}
return false;
}
/**
* 查找数组中元素e的位置
* @param e 查找元素
* @return 成功返回元素下标,不成功返回-1
*/
public int find(E e) {
for (int i = 0; i < size; i++) {
//比较是否为同一对象
if (data[i].equals(e)) {
return i;
}
}
return -1;
}
/**
* 删除index位置的元素,返回index中的元素
* @param index
* @return
*/
public E remove(int index) {
if (index < 0 || index >= size) {
throw new IllegalArgumentException("Get failed.Index is illegal.");
}
E res = data[index];
for (int i = index + 1; i < size; i++) {
data[i - 1] = data[i];
}
size--;
data[size] = null; //loitering object != memory leak
//减少容量,不能创建数组大小为0
if (size == data.length / 4 && data.length / 2 != 0) {
resize(data.length / 2);
}
return res;
}
/**
* 删除数组首元素
* @return
*/
public E removeFirst() {
return remove(0);
}
/**
* 删除数组末尾元素
*/
public E removeLast() {
return remove(size - 1);
}
/**
* 删除数组中指定元素
* @param e
* @return
*/
public boolean removeElement(E e) {
int index = find(e);
if (index != -1) {
remove(index);
return true;
}
return false;
}
/**
* @return 返回数组大小、容量
*/
@Override
public String toString() {
StringBuilder res = new StringBuilder();
res.append(String.format("Array: size = %d , capacity = %d\n", size, data.length));
res.append('[');
for (int i = 0; i < size; i++) {
res.append(data[i]);
if (i != size - 1) {
res.append(", ");
}
}
res.append(']');
return res.toString();
}
/**
* 数组扩容
* @param newCapacity
*/
private void resize(int newCapacity) {
E[] newData = (E[])new Object[newCapacity];
for (int i = 0; i < size; i++) {
newData[i] = data[i];
}
data = newData;
}
}
定义队列的接口
Queue.java
/**
* Queue
*
* @author : lao
* @date : 2019/11/3 12:45
*/
public interface Queue<E> {
int getSize();
boolean isEmpty();
void enqueue(E e);
E dequeue();
E getFront();
}
普通队列
ArrayQueue.java
/**
* ArrayQueue
*
* @author : lao
* @date : 2019/11/3 12:46
*/
public class ArrayQueue<E> implements Queue<E> {
private Array<E> array;
public ArrayQueue(int capacity) {
array = new Array<>(capacity);
}
public ArrayQueue() {
array = new Array<>();
}
@Override
public int getSize() {
return array.getSize();
}
@Override
public boolean isEmpty() {
return array.isEmpty();
}
public int getCapacity() {
return array.getCapacity();
}
/**
* 添加元素
*/
@Override
public void enqueue(E e) {
array.addLast(e);
}
/**
* 取出元素
* @return
*/
@Override
public E dequeue() {
return array.removeFirst();
}
/**
* 查看队首元素
* @return
*/
@Override
public E getFront() {
return array.getFirst();
}
@Override
public String toString() {
StringBuilder res = new StringBuilder();
res.append("Queue: ");
res.append("front [");
for (int i = 0; i < array.getSize(); i++) {
res.append(array.get(i));
if (i != array.getSize() - 1) {
res.append(", ");
}
}
res.append("] tail");
return res.toString();
}
}
循环队列
LoopQueue.java
/**
* LoopQueue
*
* @author : lao
* @date : 2019/11/3 13:00
*/
public class LoopQueue<E> implements Queue<E> {
private E[] data; //数组
private int front; //队头
private int tail; //队尾
private int size; //容量
public LoopQueue(int capacity) {
data = (E[])new Object[capacity + 1];
front = 0;
tail = 0;
size = 0;
}
public LoopQueue() {
this(10);
}
/**
* 容量
* @return
*/
public int getCapacity() {
return data.length - 1;
}
@Override
public int getSize() {
return size;
}
@Override
public boolean isEmpty() {
return front == tail;
}
/**
* 入队
* @param e
*/
@Override
public void enqueue(E e) {
if ((tail + 1) % data.length == front) {
resize(getCapacity() * 2);
}
data[tail] = e;
tail = (tail + 1) % data.length;
size++;
}
/**
* 出队
* @return
*/
@Override
public E dequeue() {
if (isEmpty()) {
throw new IllegalArgumentException("Cannot dequeue from an queue.");
}
E ret = data[front];
data[front] = null;
front = (front + 1) % data.length;
size--;
if (size == getCapacity() / 4 && getCapacity() / 2 != 0) {
resize(getCapacity() / 2);
}
return ret;
}
/**
* 获取队头元素
* @return
*/
@Override
public E getFront() {
if (isEmpty()) {
throw new IllegalArgumentException("Queue is empty.");
}
return data[front];
}
/***
* 队列扩容或缩减
* @param newCapacity 扩容或缩减大小
*/
private void resize(int newCapacity) {
E[] newData = (E[])new Object[newCapacity + 1];
for (int i = 0; i < size; i++) {
newData[i] = data[(i + front % data.length)];
}
data = newData;
front = 0;
tail = size;
}
@Override
public String toString() {
StringBuilder res = new StringBuilder();
res.append(String.format("Queue: size = %d, capacity = %d\n", size, getCapacity()));
res.append("front [");
for (int i = front ; i != tail; i = (i + 1) % data.length) {
res.append(data[i]);
if ((i + 1) % data.length != tail) {
res.append(", ");
}
}
res.append("] tail");
return res.toString();
}
public static void main(String[] args) {
LoopQueue<Integer> queue = new LoopQueue<>();
for (int i = 0; i < 10; i++) {
queue.enqueue(i);
System.out.println(queue);
if ( i % 3 == 2) {
queue.dequeue();
System.out.println(queue);
}
System.out.println("---");
}
}
}
队列的运行比较
Main.java
import java.util.Random;
public class Main {
private static double testQueue(Queue<Integer> q, int opCount) {
long startTime = System.nanoTime();
Random random = new Random();
for (int i = 0; i < opCount; i++) {
q.enqueue(random.nextInt(Integer.MAX_VALUE));
}
for (int i = 0; i < opCount; i++) {
q.dequeue();
}
long endTime = System.nanoTime();
return (endTime - startTime) / 1000000000.0;
}
public static void main(String[] args) {
int opCount = 100000;
ArrayQueue<Integer> arrayQueue = new ArrayQueue<>();
double time1 = testQueue(arrayQueue, opCount);
System.out.println("ArrayQueue, time: " + time1 + "s");
LoopQueue<Integer> loopQueue = new LoopQueue<>();
double time2 = testQueue(loopQueue, opCount);
System.out.println("LoopQueue, time: " + time2 + "s");
}
}
执行比较的结果: