数据结构(List) — ArrayQueue 源码分析

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Love667767/article/details/80365068


一、概述

版本: JDK1.8

ArrayQueue 是一个循环队列(Queue),继承了 AbstractList 抽象类,内部通过数组的方式来实现。


二、类图

这里写图片描述


三、源码分析

public class ArrayQueue<T> extends AbstractList<T> {

    private int capacity; // 当前队列的大小,不可变;
    private T[] queue;    // 队列数组;
    private int head;     // 队列首个元素所在的位置;
    private int tail;     // 队列最后一个元素所在的位置;

    public ArrayQueue(int capacity) {
        // 思考:这里为什么需要在给定容量上加1;
        this.capacity = capacity + 1;
        this.queue = newArray(capacity + 1);
        this.head = 0;
        this.tail = 0;
    }

    // 队列扩容
    public void resize(int newcapacity) {
        int size = size();
        if (newcapacity < size)
            throw new IndexOutOfBoundsException("Resizing would lose data");
        newcapacity++;
        if (newcapacity == this.capacity)
            return;
        T[] newqueue = newArray(newcapacity);
        // 注意:这里将原队列(数组)的数据拷贝到新的队列(数组)中
        for (int i = 0; i < size; i++)
            // 重写了get()方法,实现了从原队列的头部获取数据;
            newqueue[i] = get(i);
        this.capacity = newcapacity;
        this.queue = newqueue;
        // 将队列的头移回到第一个位置;
        this.head = 0;
        // 将尾指针指向当前队列最后一个元素的下一个位置;
        this.tail = size;
    }

    @SuppressWarnings("unchecked")
    private T[] newArray(int size) {
        return (T[]) new Object[size];
    }

    public boolean add(T o) {
        queue[tail] = o;
        // 通过除余来实现下标的循环;
        int newtail = (tail + 1) % capacity;
        if (newtail == head)
            throw new IndexOutOfBoundsException("Queue full");
        tail = newtail;
        return true; // we did add something
    }

    public T remove(int i) {
        if (i != 0)
            throw new IllegalArgumentException("Can only remove head of queue");
        if (head == tail)
            throw new IndexOutOfBoundsException("Queue empty");
        T removed = queue[head];
        queue[head] = null;
        // 通过除余来实现下标的循环;
        head = (head + 1) % capacity;
        return removed;
    }

    // 重写了List的get()方法,按照队列顺序依次获取元素;
    public T get(int i) {
        int size = size();
        if (i < 0 || i >= size) {
            final String msg = "Index " + i + ", queue size " + size;
            throw new IndexOutOfBoundsException(msg);
        }
        // 通过除余来实现下标的循环;
        int index = (head + i) % capacity;
        return queue[index];
    }

    // 重写了Collection.size()方法,计算当前队列中的元素个数;
    public int size() {
        // Can't use % here because it's not mod: -3 % 2 is -1, not +1.
        int diff = tail - head;
        if (diff < 0)
            diff += capacity;
        return diff;
    }
}

循环队列的两种情况:
这里写图片描述


四、小结

优点:
ArrayQueue 是一个循环队列,内部是通过数组实现的,同时 增加删除 元素不会引起内部数组的拷贝,效率较高。

缺点:
由于数组大小是提前确定的,也无法自动扩容(可以调用 resize() 进行手动扩容),因此不太适合当前队列个数未知的情况。

问题:

  1. 为什么在初始化循环队列时,在给定的容量大小上加1?

    因为初始化时,队列内没有元素,此时 head=0; tail=0; ,当有一个元素添加进去后,tail 就要向后偏移一位(会有一个位移差),在这个容器中需要多一位来存放这个 tail 指针。

猜你喜欢

转载自blog.csdn.net/Love667767/article/details/80365068