ArrayList 剖析

ArrayList

基于 JDK 1.8。
ArrayList 类的继承关系:
这里写图片描述

实现的接口

Collection

Collection 接口表示一个数据集合,数据间没有位置或顺序的概念,接口定义为:

public interface Collection<E> extends Iterable<E> {
   
    int size();
    boolean isEmpty();
    boolean contains(Object o);
    Iterator<E> iterator();
    Object[] toArray();
    <T> T[] toArray(T[] a);
    boolean add(E e);
    boolean remove(Object o);
    boolean containsAll(Collection<?> c);
    boolean addAll(Collection<? extends E> c);
    boolean removeAll(Collection<?> c);
    boolean retainAll(Collection<?> c);
    void clear();
    boolean equals(Object o);
    int hashCode();
}

这几个xxxAll()方法的含义基本也是可以顾名思义的,addAll添加,removeAll删除,containsAll检查是否包含了参数容器中的所有元素,只有全包含才返回true,retainAll**只保留参数容器中的元素**,其他元素会进行删除。

有一个抽象类AbstractCollection对这几个方法都提供了默认实现,实现的方式就是利用迭代器方法逐个操作,比如说,我们看removeAll方法,代码为:

public boolean removeAll(Collection<?> c) {
    Objects.requireNonNull(c);
    boolean modified = false;
    Iterator<?> it = iterator();
    while (it.hasNext()) {
        if (c.contains(it.next())) {
            it.remove();
            modified = true;
        }
    }
    return modified;
}

ArrayList继承了AbstractList,而AbstractList又继承了AbstractCollection,ArrayList对其中一些方法进行了重写,以提供更为高效的实现。

List

List表示有顺序或位置的数据集合,它扩展了Collection 接口,增加的主要方法有:

boolean addAll(int index, Collection<? extends E> c);

E get(int index);

E set(int index, E element);

void add(int index, E element);

E remove(int index);

int indexOf(Object o);

int lastIndexOf(Object o);

ListIterator<E> listIterator();

ListIterator<E> listIterator(int index);

List<E> subList(int fromIndex, int toIndex);

RandomAccess

RandomAccess的定义为:

public interface RandomAccess {
   
}

没有定义任何代码。这种没有任何代码的接口被称之为标记接口,用于声明类的一种属性。

该接口部分 javadoc:The primary purpose of this interface is to allow generic algorithms to alter their behavior to provide good performance when applied to either random or sequential access lists.

即实现了RandomAccess接口的类表示可以随机访问,可随机访问就是可以根据索引值就可以直接定位到具体的元素。并且一些通用的算法代码中,它可以根据这个声明而选择效率更高的实现。例如,Collections类中有一个方法binarySearch,在List中进行二分查找,它的实现代码就根据list是否实现了RandomAccess而采用不同的实现机制:

public static <T>
int binarySearch(List<? extends Comparable<? super T>> list, T key) {
    if (list instanceof RandomAccess || list.size()<BINARYSEARCH_THRESHOLD)
        return Collections.indexedBinarySearch(list, key);
    else
        return Collections.iteratorBinarySearch(list, key);
}

Serializable

它也是一个标记接口。实现该接口的类是可序列化的。没有实现此接口的类将不能使它们的任意状态被序列化或逆序列化。

构造函数

ArrayList是一个泛型容器,新建ArrayList需要实例化泛型参数:

ArrayList<Integer> intList = new ArrayList<>(10);
ArrayList<Double> doubleList = new ArrayList<>();
ArrayList<String> str
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ArrayList的时间复杂度分析主要包括插入、删除和访问操作的时间复杂度。 对于插入和删除操作,如果是在数组的末尾进行操作,时间复杂度为O(1),因为只需要将元素添加到数组的末尾或者从数组的末尾删除元素。然而,如果是在数组的中间进行插入或删除操作,需要将后面的元素向后移动或者向前移动,所以时间复杂度为O(n),其中n是数组的长度。 对于访问操作,由于ArrayList底层使用数组实现,可以直接通过索引访问数组中的元素,所以时间复杂度为O(1)。 另外,当ArrayList需要扩容时,会创建一个新的数组,并将原有数组中的元素拷贝到新数组中。根据引用中的介绍,ArrayList的扩容时间复杂度为O(n),其中n是数组的长度。 综上所述,ArrayList的插入和删除操作的时间复杂度为O(n),访问操作的时间复杂度为O(1),扩容操作的时间复杂度为O(n)。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [算法 | Java中ArrayList扩容时时间复杂度是多少?](https://blog.csdn.net/BASK2311/article/details/128464628)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [[集合]ArrayList及LinkedList源码和时间复杂度](https://blog.csdn.net/weixin_39552097/article/details/120913160)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值