源码分析-java-AbstractSequentialList
来源:互联网 发布:mysql递归 编辑:程序博客网 时间:2024/06/16 07:22
AbstractSequentialList
API文档
这个类提供了一个基本的List接口实现,为实现序列访问的数据储存结构的提供了所需要的最小化的接口实现。对于支持随机访问数据的List比如数组,应该优先使用AbstractList。
这里类是AbstractList类中与随机访问类相对的另一套系统,采用的是在迭代器的基础上实现的get、set、add和remove方法。
为了实现这个列表。仅仅需要拓展这个类,并且提供ListIterator和size方法。
对于不可修改的List,编程人员只需要实现Iterator的hasNext、next和hasPrevious、previous和index方法
对于可修改的List还需要额外实现Iterator的的set的方法
对于大小可变的List,还需要额外的实现Iterator的remove和add方法
AbstractSequentiaList和其他RandomAccess主要的区别是AbstractSequentiaList的主要方法都是通过迭代器实现的。而不是直接实现的(不通过迭代器,比如ArrayList实现的方式是通过数组实现的。)因此对于一些可以提供RandomAccess的方法,直接使用方法可能更快。因为迭代器移动需要一定代价。
源码分析
AbstractSequentialList方法的实现并没有特别需要注意的地方。主要是因为由于大部分的方法均是通过listIterator(int)的方法实现。但是listIterator(int)方法是一个抽象方法,基本实现只能从LinkedList中分析。
这里简单的分析一些编程技巧。
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); } } 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); } }
set和remove方法返回了之前的oldVal,这样可以用于一些其他的操作。
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); } }
首先代码很清晰。只需要说明三点:
一个问题是迭代器e2并不是要求是ListIterator,而仅仅是Iterator。换句话说并不对容器类型做要求。这也是利用了迭代器的优点之一。
第二个问题是这里设定了一个modified的变量。用来表示原表是否被更改。这样即使更改失败了,也可以通过返回值来判断是否对原进行了修改。
ListIterator<E> e1 = listIterator(index); Iterator<? extends E> e2 = c.iterator();
第三个问题是通用的问题,首先是这里所有的方法都采用了这样的结构
try { } catch (NoSuchElementException exc) { throw new IndexOutOfBoundsException("Index: "+index); }
这里将一个RuntimeException的未检查错误转换成了一个检查错误IndexOutOfBoundsException然后抛出。根据我目前查到的解释。大部分是说这样做是为了帮助将编程人员更好的定位问题。
- 源码分析-java-AbstractSequentialList
- AbstractSequentialList源码分析
- java集合分析(8):AbstractSequentialList
- 解析java.util集合类源码(AbstractSequentialList)
- Java集合之AbstractSequentialList
- java.util.AbstractSequentialList<E>
- AbstractSequentialList抽象类源码解析
- AbstractSequentialList
- AbstractSequentialList
- 5-3、java.util.AbstractSequentialList
- java.util.AbstractSequentialList学习笔记
- JDK源码阅读之List和AbstractSequentialList
- 共同学习Java源代码--数据结构--AbstractSequentialList类
- Java 集合深入理解(8):AbstractSequentialList
- Java 集合深入理解(8):AbstractSequentialList
- Java 集合深入理解(8):AbstractSequentialList
- Java常见集合框架(四):List之AbstractSequentialList、LinkedList
- AbstractSequentialList说明
- Android Databinding 从入门到转行(二)添加点击事件
- 2sum,3sum and 和为s的连续正数序列
- 读取android真机里面的文件的方法
- 角色的移动和转身
- 加油!加油!加油!我相信你一定行!
- 源码分析-java-AbstractSequentialList
- Ubuntu 16.04 编译安装 SkyEye 1.3.5
- C++Builder编译链接错误LME288的解决
- OpenCV初级教程
- fopen函数使用
- 多网卡情况下如何正确获得IP
- 1017. A除以B (20)--做题纪录
- poj 1987 Distance Statistics 点分治
- bzoj 3894(最小割)