ArrayDeque

ArrayDeque

ArrayDeque类是双端队列的实现类。支持从头部尾部获取、删除、新增元素等操作。它是通过数组实现的。

初始的时候,默认大小为8,内存不溢出情况下可以无限扩容

 /**
   * The minimum capacity that we'll use for a newly created deque.
   * Must be a power of 2.
   */
private static final int MIN_INITIAL_CAPACITY = 8;

起初的时候, head指向数组0位置处,tail指向数组length - 1位置处

head–> 1 <–tail

在头部新增的时候

head = -1 & 7 = 7 ,在7的位置新增,以后每次新增在head - 1(6,5,4…)位置.

 public void addFirst(E e) {
        if (e == null)
            throw new NullPointerException();
        elements[head = (head - 1) & (elements.length - 1)] = e;
        if (head == tail)
            doubleCapacity();
    }

在尾部新增的时候,

tail = 8 & 7 = 0,在0的位置新增,以后每次新增在tail + 1(1,2,3…)位置新增

public void addLast(E e) {
        if (e == null)
            throw new NullPointerException();
        elements[tail] = e;
        if ( (tail = (tail + 1) & (elements.length - 1)) == head)
            doubleCapacity();
    }

扩容的时候,(以扩容到16位例)

private void doubleCapacity() {
        assert head == tail; 
        int p = head;
        int n = elements.length;
        int r = n - p; // number of elements to the right of p
        int newCapacity = n << 1;
        if (newCapacity < 0)
            throw new IllegalStateException("Sorry, deque too big");
        Object[] a = new Object[newCapacity];
        System.arraycopy(elements, p, a, 0, r);
        System.arraycopy(elements, 0, a, r, p);
        elements = (E[])a;
        head = 0;
        tail = n;
    }

当队列头部再插入一个元素,队列就会首位相接的时候 head - 1 == tail ,表示队列满了,需要扩容,每次扩容为之前的2倍。
这里写图片描述
从head到原内存数组末尾的元素放到新内存中从0开始的位置,原数组0到tail的部分跟在后面,如下图
这里写图片描述
之后再尾部新增时 tail继续 +1后移, 头部新增的时候head - 1前移。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值