Java容器 列表(List) 概览

本文详细介绍了Java中List接口的概念及其实现方式,包括ArrayList和LinkedList的特点。此外,还讲解了List接口提供的方法及其迭代器ListIterator的使用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

列表是一组元素可以重复,可以有多个空元素的元素集合。Java容器框架中接口List,抽象类AbstractList以及各种子类ArrayList, LinkedList等都是列表的实现。
类框架图如下:
这里写图片描述

List是有序序列的接口规定。该容器的顺序与元素插入的顺序保持一次,可以通过整数索引访问元素。
List中除了继承Collection接口中声明的add, remove, get等方法,还规定了额外的add, remove, get方法(通过整数索引)。

    public interface List<E> extends Collection<E> {
        ...
        E get(int index);
        void add(int index, E element);
        E remove(int index);
        ...
    }

List还提供了特殊的迭代器,ListIterator。 这个迭代器可以双向访问,且还提供插入,替换,删除等操作。List还提供了一个可以获取一个在指定位置开始的迭代器。

    public interface List<E> extends Collection<E> {
        ...
        ListIterator<E> listIterator();
        //在index位置开始的迭代器
        ListIterator<E> listIterator(int index);
        ...
    }

AbstractList提供了List方法的默认实现,这样List的所有实现类无需都实现List接口中声明的方法。在List和需要实现List接口的实现类中作为一个桥梁作用。

迭代器实现

    public Iterator<E> iterator() {
        //Itr是AbstractList内部实现的一个迭代器类
         return new Itr();
    }

     private class Itr implements Iterator<E> {
        int cursor = 0;
        int lastRet = -1;
        int expectedModCount = modCount;

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

        public E next() {
            checkForComodification();
            try {
                int i = cursor;
                E next = get(i);
                lastRet = i;
                cursor = i + 1;
                return next;
            } catch (IndexOutOfBoundsException e) {
                checkForComodification();
                throw new NoSuchElementException();
            }
        }

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

            try {
                AbstractList.this.remove(lastRet);
                if (lastRet < cursor)
                    cursor--;
                lastRet = -1;
                expectedModCount = modCount;
            } catch (IndexOutOfBoundsException e) {
                throw new ConcurrentModificationException();
            }
        }

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

AbstractList 中的默认迭代器实现,使用cursor下一个访问的位置。当cursor == size() 时说明已经到了最后一个元素,没有next()。lastRet记录最后操作的索引位置。next() 获取的是get(cursor)上的元素。remove调用的是lst上实现的remove方法,remove后cursor–;lastRet=-1。

还提供了ListIterator的默认实现

     public ListIterator<E> listIterator() {
        return listIterator(0);
    }

    public ListIterator<E> listIterator(final int index) {
        rangeCheckForAdd(index);

        return new ListItr(index);
    }
    //ListItr也是AbstractList的内部类。是升级的迭代器
     private class ListItr extends Itr implements ListIterator<E> {
        ListItr(int index) {
            cursor = index;
        }

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

        public E previous() {
            checkForComodification();
            try {
                int i = cursor - 1;
                E previous = get(i);
                lastRet = cursor = i;
                return previous;
            } catch (IndexOutOfBoundsException e) {
                checkForComodification();
                throw new NoSuchElementException();
            }
        }

        public int nextIndex() {
            return cursor;
        }

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

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

            try {
                AbstractList.this.set(lastRet, e);
                expectedModCount = modCount;
            } catch (IndexOutOfBoundsException ex) {
                throw new ConcurrentModificationException();
            }
        }

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

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

ListItr 是一个升级版的迭代器,该迭代器提供了双向访问(可向前向后移动游标)的功能,并且还提供了set, add 的功能。

sublist默认实现

    public List<E> subList(int fromIndex, int toIndex) {
        return (this instanceof RandomAccess ?
        //如果是随机访问的列表实现,使用RandomAccessSubList。否者就实现SubList
          new RandomAccessSubList<>(this, fromIndex, toIndex) :
                new SubList<>(this, fromIndex, toIndex));
    }

//可以随机访问的一个子序列
class RandomAccessSubList<E> extends SubList<E> implements RandomAccess {
    RandomAccessSubList(AbstractList<E> list, int fromIndex, int toIndex) {
        super(list, fromIndex, toIndex);
    }

    public List<E> subList(int fromIndex, int toIndex) {
        return new RandomAccessSubList<>(this, fromIndex, toIndex);
    }
}

SubList的操作都在原来列表上的操作,也就是说,通过List获得的SubList并不是一个新的序列,只是原来List的一个视图。所有在SubList上的操作都会反应到原来的List上。


AbstractSequenceList 是顺序访问的桥梁实现。


List最常用的实现是
ArrayList:基于数组的实现,随机访问
LinkedList: 基于链表的实现,顺序访问

啊啊啊啊啊,累累累累累,坚持一下坚持一下坚持一下~~~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值