0
点赞
收藏
分享

微信扫一扫

Boost 网络库

RockYoungTalk 2024-06-19 阅读 44

目录


一、ArrayList源码

在这里插入图片描述

首先看一下ArrayList的继承关系结构图
在这里插入图片描述

ArrayList的类声明

public class ArrayList<E> extends AbstractList<E>
        implements List<E>, RandomAccess, Cloneable, java.io.Serializable
{

1.1 迭代器

ArrayList源码中有一个内部类ListItr
ListItr是什么?---->官方注释:AbstractList的优化版本:ListItr

    /**
     * An optimized version of AbstractList.ListItr
     */
    private class ListItr extends Itr implements ListIterator<E> {
        ListItr(int index) {
            super();
            cursor = index;
        }

        public boolean hasPrevious() {
            return cursor != 0;
        }

        public int nextIndex() {
            return cursor;
        }

        public int previousIndex() {
            return cursor - 1;
        }

        @SuppressWarnings("unchecked")
        public E previous() {
            checkForComodification();
            int i = cursor - 1;
            if (i < 0)
                throw new NoSuchElementException();
            Object[] elementData = ArrayList.this.elementData;
            if (i >= elementData.length)
                throw new ConcurrentModificationException();
            cursor = i;
            return (E) elementData[lastRet = i];
        }

        public void set(E e) {
            if (lastRet < 0)
                throw new IllegalStateException();
            checkForComodification();

            try {
                ArrayList.this.set(lastRet, e);
            } catch (IndexOutOfBoundsException ex) {
                throw new ConcurrentModificationException();
            }
        }

        public void add(E e) {
            checkForComodification();

            try {
                int i = cursor;
                ArrayList.this.add(i, e);
                cursor = i + 1;
                lastRet = -1;
                expectedModCount = modCount;
            } catch (IndexOutOfBoundsException ex) {
                throw new ConcurrentModificationException();
            }
        }
    }

可以看到 ListItr 继承自Itr,那么Itr长啥样子呢?
官方注释:Itr,一个AbstractList.Itr的优化版本

    /**
     * An optimized version of AbstractList.Itr
     */
    private class Itr implements Iterator<E> {
        int cursor;       // index of next element to return
        int lastRet = -1; // index of last element returned; -1 if no such
        int expectedModCount = modCount;

        // prevent creating a synthetic constructor
        Itr() {}

        public boolean hasNext() {
            return cursor != size;
        }

        @SuppressWarnings("unchecked")
        public E next() {
            checkForComodification();
            int i = cursor;
            if (i >= size)
                throw new NoSuchElementException();
            Object[] elementData = ArrayList.this.elementData;
            if (i >= elementData.length)
                throw new ConcurrentModificationException();
            cursor = i + 1;
            return (E) elementData[lastRet = i];
        }

        public void remove() {
            if (lastRet < 0)
                throw new IllegalStateException();
            checkForComodification();

            try {
                ArrayList.this.remove(lastRet);
                cursor = lastRet;
                lastRet = -1;
                expectedModCount = modCount;
            } catch (IndexOutOfBoundsException ex) {
                throw new ConcurrentModificationException();
            }
        }

        @Override
        public void forEachRemaining(Consumer<? super E> action) {
            Objects.requireNonNull(action);
            final int size = ArrayList.this.size;
            int i = cursor;
            if (i < size) {
                final Object[] es = elementData;
                if (i >= es.length)
                    throw new ConcurrentModificationException();
                for (; i < size && modCount == expectedModCount; i++)
                    action.accept(elementAt(es, i));
                // update once at end to reduce heap write traffic
                cursor = i;
                lastRet = i - 1;
                checkForComodification();
            }
        }

        final void checkForComodification() {
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
        }
    }

1.1.1 Itr源码浅析

可以在上面看到Itr实现了Iterator迭代器接口,实现了四个方法hasNext()next()remove()forEachRemaining()

1.1.2 ListItr源码浅析

Itr仅仅是实现了迭代器接口, 而ListItr继承自 Itr 类并实现了 ListIterator< E> 接口,用于提供对 ArrayList 的列表迭代器功能。以下是对代码中关键部分的详细解释:

这段代码实现了 ListIterator 的功能,允许在 ArrayList 中进行双向迭代,并提供了添加和替换元素的功能。同时,代码中也考虑了并发修改的情况,确保操作的安全性和一致性。

看一下迭代器的创建

    /**
     * Returns a list iterator over the elements in this list (in proper
     * sequence), starting at the specified position in the list.
     * The specified index indicates the first element that would be
     * returned by an initial call to {@link ListIterator#next next}.
     * An initial call to {@link ListIterator#previous previous} would
     * return the element with the specified index minus one.
     *
     * <p>The returned list iterator is <a href="#fail-fast"><i>fail-fast</i></a>.
     *
     * @throws IndexOutOfBoundsException {@inheritDoc}
     */
        public ListIterator<E> listIterator(int index) {
        rangeCheckForAdd(index);
        return new ListItr(index);
    }

    /**
     * Returns a list iterator over the elements in this list (in proper
     * sequence).
     *
     * <p>The returned list iterator is <a href="#fail-fast"><i>fail-fast</i></a>.
     *
     * @see #listIterator(int)
     */
    public ListIterator<E> listIterator() {
        return new ListItr(0);
    }

两个重载方法 listIterator() 和 listIterator(int index),用于返回一个 ListIterator 对象,从列表中的指定位置或者从列表的起始位置开始迭代元素。


1.2 常用方法

常用方法 无非增删改查

在这里插入图片描述

    public void add(int index, E element) {
        rangeCheckForAdd(index);
        modCount++;
        final int s;
        Object[] elementData;
        if ((s = size) == (elementData = this.elementData).length)
            elementData = grow();
        System.arraycopy(elementData, index,
                         elementData, index + 1,
                         s - index);
        elementData[index] = element;
        size = s + 1;
    }

    public E remove(int index) {
        Objects.checkIndex(index, size);
        final Object[] es = elementData;

        @SuppressWarnings("unchecked") E oldValue = (E) es[index];
        fastRemove(es, index);

        return oldValue;
    }
     private void fastRemove(Object[] es, int i) {
     modCount++;
     final int newSize;
     if ((newSize = size - 1) > i)
         System.arraycopy(es, i + 1, es, i, newSize - i);
     es[size = newSize] = null;
    }

    public E set(int index, E element) {
        Objects.checkIndex(index, size);
        E oldValue = elementData(index);
        elementData[index] = element;
        return oldValue;
    }

    public E get(int index) {
        Objects.checkIndex(index, size);
        return elementData(index);
    }

1.3 System.arraycopy

常用方法中只有增删操作使用了System.arraycopy

 public static native void arraycopy(Object src,  int  srcPos,
                                        Object dest, int destPos,
                                        int length);

System.arraycopy是一个native方法,

参数解释:

对于增删操作中的 System.arraycopy 的使用场景:

System.arraycopy 是一种底层的数组复制方法,效率较高,但只能用于数组之间的元素复制,不能用于集合之间的元素复制。在增删操作中,通过 System.arraycopy 可以较方便地实现元素的移动和填充,同时能够保证数据的完整性和位置的正确性。


1.4 ArrayList 的创建方式

public class ArrayList<E> {

	public ArrayList()
	
	public ArrayList(int initialCapacity) 
	
	public ArrayList(Collection<? extends E> c) 
	
}

通过无参构造创建时,数组的默认初始容量是10

通过指定长度参构造创建时,数组的初始容量是指定的长度

第三种在创建时,会将传入的集合数据存储到数组中,数组的初始容量是传入集合的长度


二、引申问题

2.1 ArrayList的大小是如何增加的?

在add方法中,添加元素,增长ArrayList的长度

    public void add(int index, E element) {
        rangeCheckForAdd(index);
        modCount++;
        final int s;
        Object[] elementData;
        if ((s = size) == (elementData = this.elementData).length)
            elementData = grow();
        System.arraycopy(elementData, index,
                         elementData, index + 1,
                         s - index);
        elementData[index] = element;
        size = s + 1;
    }
    private Object[] grow() {
        return grow(size + 1);
    }
    /**
     * Increases the capacity to ensure that it can hold at least the
     * number of elements specified by the minimum capacity argument.
     *
     * @param minCapacity the desired minimum capacity
     * @throws OutOfMemoryError if minCapacity is less than zero
     */
    private Object[] grow(int minCapacity) {
        int oldCapacity = elementData.length;
        if (oldCapacity > 0 || elementData != DEFAULTCAPACITY_EMPTY_ELEMENTDATA) {
            int newCapacity = ArraysSupport.newLength(oldCapacity,
                    minCapacity - oldCapacity, /* minimum growth */
                    oldCapacity >> 1           /* preferred growth */);
            return elementData = Arrays.copyOf(elementData, newCapacity);
        } else {
            return elementData = new Object[Math.max(DEFAULT_CAPACITY, minCapacity)];
        }
    }

在扩容过程中,会根据当前数组的容量和需要扩容的最小增量以及首选增量来计算新的容量大小,然后使用 Arrays.copyOf 方法创建一个新的数组,并将原数组的内容复制到新数组中,最后将新数组赋值给 elementData。完成了列表的扩容操作,确保ArrayList 在添加元素时能够保持合理的容量并避免频繁扩容。


2.2 什么情况下你会使用ArrayList

使用List无非就是使用它的常用方法,而从ArrayList的增删改查源码中我们可以看出,增删过程会将数组的元素拷贝到新数组中来,再添加新数据;由于扩容需要新建数组且拷贝之前到元素到新数组中,所以说数据越多,操作越慢。


2.3 在索引中ArrayList的增加或者删除某个对象的运行过程,效率很低吗?解释一下为什么

从源码已经看出按索引增加或者删除某个对象,会使用底层的arraycopy挪动数组,效率相对较低,特别是在操作的位置接近列表的开始处时。这主要是由于数组的特性决定的

  • 数组的结构:ArrayList 内部使用数组作为数据存储结构,数组是一种紧凑的数据结构,元素在内存中是连续存储的。当在数组中某个位置插入或删除元素时,需要将该位置后面的所有元素向后或向前移动,以保持索引的连续性。
  • 移动元素的开销:在数组中,移动元素的操作是比较耗时的,因为需要将大量元素复制到新的位置。如果需要在数组的开始处插入或删除元素,那么所有后续元素都需要移动,这个开销是随索引位置增大而增大的。
  • 时间复杂度:在 ArrayList 中按索引增加或删除元素的时间复杂度为 O(n),其中 n 是列表元素的总数。这是因为需要移动大量元素来维持索引的连续性,复杂度与数组中需要移动的元素数量成正比。
  • 相对于末尾的操作:相比较于在 ArrayList 的末尾增加或删除元素,按索引操作效率更低。在末尾增加或删除元素时,时间复杂度为 O(1),因为不需要移动元素,只需修改数组的长度即可。

ArrayList 在按索引增加或删除元素时效率较低,特别是在操作的位置接近列表的起始处时。如果对列表的操作需求包括频繁的按索引增加或删除元素,可能会影响程序的性能。在这种情况下,考虑使用其他数据结构如 LinkedList 等可能会更加高效。


2.4 ArrayList如何顺序删除节点

可以使用迭代器(Iterator)来遍历列表并删除满足特定条件的节点。

  • 获取 ArrayList 的迭代器对象:通过调用 ArrayList 的 iterator() 方法获取一个迭代器对象,用于遍历 ArrayList 中的元素。
  • 使用迭代器遍历 ArrayList:通过迭代器的 hasNext() 和 next() 方法来遍历 ArrayList 中的元素。在遍历过程中,可以判断当前元素是否满足删除条件。
  • 删除满足条件的节点:如果发现某个节点(元素)满足删除条件,可以调用迭代器的 remove() 方法来删除当前节点。注意:在使用迭代器的 remove() 方法之前必须先调用 next() 方法来指向要删除的元素。
  • 循环遍历直至完成:重复执行第2步和第3步,直到遍历完成所有的节点。

示例

import java.util.ArrayList;
import java.util.Iterator;

public class RemoveElementsInArrayList {
    public static void main(String[] args) {
        ArrayList<Integer> numbers = new ArrayList<>();
        numbers.add(1);
        numbers.add(2);
        numbers.add(3);
        numbers.add(4);
        numbers.add(5);

        Iterator<Integer> iterator = numbers.iterator();
        while (iterator.hasNext()) {
            int number = iterator.next();
            if (number % 2 == 0) {  // 删除偶数节点
                iterator.remove();
            }
        }

        System.out.println(numbers);
    }
}

2.5 ArrayList的遍历方法

适用迭代器的next()方法遍历。


三、总结

数组的两个概念:大小、容量

ArrayList 和 LinkedList 的区别

分类ArrayListLinkedList
数据结构数组链表
查询速度
增删速度不一定不一定
存储相同数据所需要的空间
应用场景查询较多增删较多

参考链接:
java集合框架05——ArrayList和LinkedList的区别

举报

相关推荐

0 条评论