java集合(ArrayList)

用途和解释

Java.util.ArrayList类是一个动态数组队列,可以随时从链表中添加或删除一个元素。ArrayList实现了List接口并且可以动态改变大小的。当我们不知道数据元素的个数时,就可使用ArrayList;与Java中的数组相比,它的容量能动态增长。它继承于AbstractList,实现了List, RandomAccess, Cloneable, java.io.Serializable这些接口。和Vector不同,ArrayList中的操作不是线程安全的。所以,建议在单线程中才使用ArrayList,而在多线程中可以选择Vector

实例

List<String> lists = new ArrayList<String>();(其中的String可以改为integer,double等类型)

注:ArrayList对象只能存放对象,不能存放基础数据类型的数据。

源码分析结构

源码继承结构
在这里插入图片描述
ArrayList 继承了 AbstractList 抽象类,实现了 List 接口、RandomAccess 接口、Cloneable 接口、java.io.Serializable 接口

List 接口

有序的集合,存储元素和取出元素的顺序是一致的
允许存储重复的元素(包括空)
上诉可以看到,List 将 Collection 中的抽象方法重新定义了一遍

在这里插入图片描述

void add(int index, E element); // 向指定索引处增加元素 0 <= index <= size
boolean addAll(int index, Collection<? extends E> c); // 向指定索引处增加集合中的所有元素 0 <= index <= size
E remove(int index); // 删除指定索引位置的元素 0 <= index < size
E set(int index, E element); // 修改指定索引处的元素 0 <= index < size
E get(int index); // 获取指定索引处的元素 0 <= index < size
int indexOf(Object o); // 获取指定元素第一次出现的索引,若不存在则返回-1
int lastIndexOf(Object o); // 获取指定元素最后一次出现的索引,若不存在则返回-1
List<E> subList(int fromIndex, int toIndex); // 返回集合中从fromIndex(含)到toIndex(不含)的视图,与原集合会相互影响
ListIterator<E> listIterator(); // 返回listIterator hasPrevious()、previous()、add(E e);
ListIterator<E> listIterator(int index); // 从指定索引处开始返回listIterator 0 <= index <= size
// jdk1.8新增方法
default void sort(Comparator<? super E> c){
   } // 根据Comparator来排序数组
default void replaceAll(UnaryOperator<E> operator) {
   } // 将该列表的每个元素替换为该运算符应用于该元素的结果

AbstractList 抽象类

在这里插入图片描述

	在 AbstractList 抽象父类中:有些方法已经被赋予了具体的实现逻辑,比如 indexOf() 和 lastIndexOf() 方法;而有些方法则需要推迟到子类中去实现,比如 get()、set()、add()、remove() 等方法
	// 维护了集合被修改的次数
	protected transient int modCount = 0;
	
	//在集合末尾添加元素,由子类实现
    public boolean add(E e) {
   
        add(size(), e);
        return true;
    }

    /**
     * {@inheritDoc}
     *
     * @throws IndexOutOfBoundsException {@inheritDoc}
     */
    abstract public E get(int index);

	// 限定了add、set、remove操作, 由子类实现
    public E set(int index, E element) {
   
        throw new UnsupportedOperationException();
    }

    public void add(int index, E element) {
   
        throw new UnsupportedOperationException();
    }

    public E remove(int index) {
   
        throw new UnsupportedOperationException();
    }


    // Search Operations

    /**
     * {@inheritDoc}
     *
     * <p>This implementation first gets a list iterator (with
     * {@code listIterator()}).  Then, it iterates over the list until the
     * specified element is found or the end of the list is reached.
     *
     * @throws ClassCastException   {@inheritDoc}
     * @throws NullPointerException {@inheritDoc}
     */
     // 从前往后找,获取指定元素在集合中第一次出现的索引,若不存在则返回-1
    public int indexOf(Object o) {
   
        ListIterator<E> it = listIterator();
        if (o==null) {
   
            while (it.hasNext())
                if (it.next()==null)
                    return it.previousIndex();
        } else {
   
            while (it.hasNext())
                if (o.equals(it.next()))
                    return it.previousIndex();
        }
        return -1;
    }

    /**
     * {@inheritDoc}
     *
     * <p>This implementation first gets a list iterator that points to the end
     * of the list (with {@code listIterator(size())}).  Then, it iterates
     * backwards over the list until the specified element is found, or the
     * beginning of the list is reached.
     *
     * @throws ClassCastException   {@inheritDoc}
     * @throws NullPointerException {@inheritDoc}
     */
     // 从后往前找,获取指定元素在集合中第一次出现的索引,若不存在则返回-1
    public int lastIndexOf(Object o) {
   
        ListIterator<E> it = listIterator(size());
        if (o==null) {
   
            while (it.hasPrevious())
                if (it.previous()==null)
                    return it.nextIndex();
        } else {
   
            while (it.hasPrevious())
                if (o.equals(it.previous()))
                    return it.nextIndex();
        }
        return -1;
    }


    // Bulk Operations

    /**
     * Removes all of the elements from this list (optional operation).
     * The list will be empty after this call returns.
     *
     * <p>This implementation calls {@code removeRange(0, size())}.
     *
     * <p>Note that this implementation throws an
     * {@code UnsupportedOperationException} unless {@code remove(int
     * index)} or {@code removeRange(int fromIndex, int toIndex)} is
     * overridden.
     *
     * @throws UnsupportedOperationException if the {@code clear} operation
     *         is not supported by this list
     */
    public void clear() {
   
        removeRange(0, size());
    }

    /**
     * {@inheritDoc}
     *
     * <p>This implementation gets an iterator over the specified collection
     * and iterates over it, inserting the elements obtained from the
     * iterator into this list at the appropriate position, one at a time,
     * using {@code add(int, E)}.
     * Many implementations will override this method for efficiency.
     *
     * <p>Note that this implementation throws an
     * {@code UnsupportedOperationException} unless
     * {@link #add(int, Object) add(int, E)} is overridden.
     *
     * @throws UnsupportedOperationException {@inheritDoc}
     * @throws ClassCastException            {@inheritDoc}
     * @throws NullPointerException          {@inheritDoc}
     * @throws IllegalArgumentException      {@inheritDoc}
     * @throws IndexOutOfBoundsException     {@inheritDoc}
     */
     // 向指定索引处增加集合中的所有元素, add由子类实现
    public boolean addAll(int index, Collection<? extends E> c) {
   
    //索引检查 index < 0 || index > size()
        rangeCheckForAdd(index)
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

诗~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值