共同学习Java源代码-数据结构-AbstractQueue

public abstract class AbstractQueue<E>  extends AbstractCollection<E>  implements Queue<E>
这个抽象类继承自AbstractCollection类 实现Queue接口


    protected AbstractQueue() {
    }


无参构造方法 protected修饰


    public boolean add(E e) {
        if (offer(e))
            return true;
        else
            throw new IllegalStateException("Queue full");
    }


add方法 就是调用offer方法 如果offer返回true 就返回true 否则抛出异常


    public E remove() {
        E x = poll();
        if (x != null)
            return x;
        else
            throw new NoSuchElementException();
    }


remove方法 


调用poll方法 判断返回值不为空 就返回poll方法的返回值 否则抛出异常






    public E element() {
        E x = peek();
        if (x != null)
            return x;
        else
            throw new NoSuchElementException();
    }


element方法和remove方法类似 不多说了


    public void clear() {
        while (poll() != null)
            ;
    }


clear方法 一直调用poll


    public boolean addAll(Collection<? extends E> c) {
        if (c == null)
            throw new NullPointerException();
        if (c == this)
            throw new IllegalArgumentException();
        boolean modified = false;
        for (E e : c)
            if (add(e))
                modified = true;
        return modified;
    }


addAll方法 将参数集合的所有元素添加进去


判断如果参数为空 抛出异常


如果参数就是本身 抛出异常


定义临时变量modified代表是否被修改 初始为false


遍历参数集合 调用add方法 如果add方法返回true 就将modified设为true 最后返回modified
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值