Java集合(6)——AbstractSequentialList源码解析

AbstractSequentialList抽象类

类图

这里写图片描述

官方文档

这里写图片描述

AbstractSequentialList抽象成员方法

这里写图片描述

AbstractSequentialList构造函数

这里写图片描述

AbstractSequentialList成员方法

这里写图片描述

1. public E get(int index)方法
    public E get(int index) {
        try {
            return listIterator(index).next();
        } catch (NoSuchElementException exc) {
            throw new IndexOutOfBoundsException("Index: "+index);
        }
    }

源码解析:

  • 功能:返回索引值为index的集合元素
  • 源码思路:调用该类的成员变量listIterator的next()方法
2. public E set(int index, E element)方法
    public E set(int index, E element) {
        try {
            ListIterator<E> e = listIterator(index);
            E oldVal = e.next();
            e.set(element);
            return oldVal;
        } catch (NoSuchElementException exc) {
            throw new IndexOutOfBoundsException("Index: "+index);
        }
    }

源码解析:

  • 功能:设置索引值为index的集合元素的值为element
  • 源码思路:利用迭代器e来实现该功能,首先将迭代器游标放置在index位置;然后获取该位置的元素,通过调用迭代器的set方法来重新设置元素的值;最后返回index位置更改之前元素的值。
3. public void add(int index, E element)方法
    public void add(int index, E element) {
        try {
            listIterator(index).add(element);
        } catch (NoSuchElementException exc) {
            throw new IndexOutOfBoundsException("Index: "+index);
        }
    }

源码解析:

  • 功能:在index位置增加一个元素,元素的值为element
  • 源码思路:使用ListIterator类型的迭代器的add()方法添加元素
4. public E remove(int index)方法
    public E remove(int index) {
        try {
            ListIterator<E> e = listIterator(index);
            E outCast = e.next();
            e.remove();
            return outCast;
        } catch (NoSuchElementException exc) {
            throw new IndexOutOfBoundsException("Index: "+index);
        }
    }

源码解析:

  • 功能:移除下标为index的元素
  • 源码思路:使用ListIterator类型的迭代器,现将迭代器的游标置于index位置;然后定义一个E类型的变量outCast,将迭代器的下一个位置的元素赋值给outCast变量;使用迭代器的remove方法将元素移除;最后返回删除的元素。
5. public boolean addAll(int index, Collection<? extends E> c)方法
    public boolean addAll(int index, Collection<? extends E> c) {
        try {
            boolean modified = false;
            ListIterator<E> e1 = listIterator(index);
            Iterator<? extends E> e2 = c.iterator();
            while (e2.hasNext()) {
                e1.add(e2.next());
                modified = true;
            }
            return modified;
        } catch (NoSuchElementException exc) {
            throw new IndexOutOfBoundsException("Index: "+index);
        }
    }

源码解析:

  • 功能:将集合c中的所有元素添加到当前集合的index位置,如list1中有元素[1,2,29],list2中有元素[-11,-10],在执行list1.addAll(1, list2);后,list1中的元素变为[1, -11, -10, 2, 29]
  • 源码思路:现有集合和将要添加元素的集合都要使用迭代器来实现该功能。第一步:得到当前集合的迭代器,且迭代器的游标置于index位置,得到添加元素集合的迭代器。第二步:使用while循环,利用迭代器将元素遍历出来,并添加到对应的位置。第三步:返回添加是否成功的结果
6. public Iterator<E> iterator()方法
    public Iterator<E> iterator() {
        return listIterator();
    }

源码解析:

  • 功能:返回该集合的迭代器
  • 源码思路:实现该方法是通过调用AbstractSequentialList类的抽象方法public abstract ListIterator<E> listIterator(int index);实现的,因为不同的子类对于迭代器的处理方式是不同的,因此只能依靠子类来实现该抽象方法。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值