LinkedList和ArrayList的删除操作

通过api文档发现LinkedList比ArrayList多个 removeFirst()和 removeLast()方法。
既然有了 remove()为何还要多造其他俩种??

看源码
LinkedList

 /**
     * Retrieves and removes the head (first element) of this list.
     *
     * @return the head of this list
     * @throws NoSuchElementException if this list is empty
     * @since 1.5
     */
    public E remove() {
        return removeFirst();
    }
/**
     * Removes and returns the first element from this list.
     *
     * @return the first element from this list
     * @throws NoSuchElementException if this list is empty
     */
    public E removeFirst() {
        final Node<E> f = first;
        if (f == null)
            throw new NoSuchElementException();
        return unlinkFirst(f);
    }

原来LinkedList的remove()就是removeFirst(),而接口 Collection是没有无参的remove(),所以这是它特有的方法,那为啥要多此一举呢?
个人认为链表结构的删除需要操作前后节点指向,但第一个没有没必要多此一举所以用了简化版的删除,而且也大大提高了遍历删集合的效率(ArrayList的我不想吐槽)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值