`
liuzhaomin
  • 浏览: 199575 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

AbstractList

 
阅读更多
/**
     * Returns an iterator over the elements in this list in proper sequence.
     *
     * <p>This implementation returns a straightforward implementation of the
     * iterator interface, relying on the backing list's {@code size()},
     * {@code get(int)}, and {@code remove(int)} methods.
     *
     * <p>Note that the iterator returned by this method will throw an
     * {@code UnsupportedOperationException} in response to its
     * {@code remove} method unless the list's {@code remove(int)} method is
     * overridden.
     *
     * <p>This implementation can be made to throw runtime exceptions in the
     * face of concurrent modification, as described in the specification
     * for the (protected) {@code modCount} field.
     *
     * @return an iterator over the elements in this list in proper sequence
     *
     * @see #modCount
     */
    public Iterator<E> iterator() {
	return new Itr();
    }

 

private class Itr implements Iterator<E> {
	/**
	 * Index of element to be returned by subsequent call to next.
	 */
	int cursor = 0;

	/**
	 * Index of element returned by most recent call to next or
	 * previous.  Reset to -1 if this element is deleted by a call
	 * to remove.
	 */
	int lastRet = -1;

	/**
	 * The modCount value that the iterator believes that the backing
	 * List should have.  If this expectation is violated, the iterator
	 * has detected concurrent modification.
	 */
	int expectedModCount = modCount;

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

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

	public void remove() {
	    if (lastRet == -1)
		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();
	}
    }

    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 == -1)
		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 {
		AbstractList.this.add(cursor++, e);
		lastRet = -1;
		expectedModCount = modCount;
	    } catch (IndexOutOfBoundsException ex) {
		throw new ConcurrentModificationException();
	    }
	}
    }

分享到:
评论

相关推荐

    ArrayList、Vector、LinkedList 的区别.docx

    首先,从继承关系来看,ArrayList 和 Vector 都继承了 AbstractList 抽象类,该抽象类提供了 List 接口的默认实现,支持随机访问。LinkedList 则继承了 AbstractSequentialList 抽象类,该抽象类提供了 List 接口的...

    Java 基础核心总结 +经典算法大全.rar

    AbstractList 和 AbstractSequentialList Vector Stack ArrayList LinkedList Queue接口Deque 接口 AbstractQueue 抽象类LinkedList ArrayDeque PriorityQueue 反射的思想及作用 反射的基本使用 获取类的 Class 对象...

    Java数据库查询结果的输出

    public class Vector extends AbstractList implements List , Cloneable , Serializable{…} 类JTable:  JTable组件是Swing组件中比较复杂的小件,隶属于javax.swing包,它能以二维表的形式显示数据。类Jtable:...

    JDK 1.5的泛型實現(Generics in JDK 1.5)

    #001 public class ArrayList&lt;E&gt; extends AbstractList&lt;E&gt; #002 implements List, RandomAccess, #003 Cloneable, java.io.Serializable #004 { #005 private transient E[] elementData; #006 private int ...

    Java系列ArrayList

    ArrayList ArrayList 类是一个可以动态修改...ArrayList 继承了 AbstractList ,并实现了 List 接口。 添加元素 访问元素 修改元素 删除元素 计算大小 迭代数组列表 其他的引用类型 ArrayList 排序 Java ArrayList 方法

    Java ArrayList

    ArrayList 继承了 AbstractList ,并实现了 List 接口。 ArrayList 类位于 java.util 包中,使用前需要引入它,语法格式如下: import java.util.ArrayList; // 引入 ArrayList 类 ArrayList objectName =new ...

    List集合之ArrayList

    ArrayList集成AbstractList抽象类,实现了List、RandomAccess、Cloneable、java.io.Serializable这四个接口,其中我们可以看到,实现了Cloneable和Serializable接口就代表着ArrayList是支持克隆和序列化的,这里有个...

    ArrayList源码分析

    它继承于AbstractList,实现了List, RandomAccess, Cloneable, java.io.Serializable的方法,我们从它的源码中可以清楚的看到 //默认的初始化容量为10 private static final int DEFAULT_CAPACITY = 10; //用于...

    javajdk源码学习-JavaSourceLearn:JDK源码学习

    java jdk源码学习 JavaSourceLearn 版本号 版本 corretto-1.8.0_275 方式 ...AbstractList 1 AbstractMap 1 AbstractSet 1 ArrayList 1 LinkedList 1 HashMap 1 Hashtable 1 HashSet 1 LinkedHashMa

    Java进阶--深入理解ArrayList实现原理

    由上可知ArrayList继承AbstractList并且实现了List和RandomAccess,Cloneable,Serializable接口。①构造方法由上面三种构造方法可知,默认情况下使用ArrayList会生成一个大小为10的Object类型的数组。也可以调用...

    超全Java集合框架讲解.md

    超全Java集合框架讲解 - 超全Java集合框架讲解 ... - AbstractList 和 AbstractSequentialList - Vector - Stack - ArrayList - LinkedList - Queue接口 - Deque 接口 - AbstractQueue 抽象类 - Lin

    大数据开发成长之路——Java基础(四)

    List被AbstractList实现,然后分为3个子类,ArrayList,LinkedList和VectorList List是一种有序链表,本身是一个泛型接口,元素可以重复,可以是 null 包含以下方法: 遍历List // for循环 List list = ...; for...

    Java源码分析:深入探讨Iterator模式

    下面我们先简单讨论一个根接口Collection,然后分析一个抽象类AbstractList和它的对应Iterator接口,并仔细研究迭代子模式的实现原理。 本文讨论的源代码版本是JDK 1.4.2,因为JDK 1.5在java.util中使用了很多泛型...

    Java Collections中的Fail Fast机制

    抽象类、AbstractList 抽象类和具体的ArrayList 的实现纵向研究了Java Collections Framework 中的Fail Fast 机制,通常的编程错误以及这些接口和类之间的关系,以有助于大家对Java Collections Framework 源代码的...

    java8源码-data-structure:这个项目是为了学习Java数据结构

    打开ArrayList的源码我们能看到ArrayList实现了List接口,扩展至AbstractList,其本质是一个可变长度的数组。 Java8中ArrayList包含注释一起一共1468行代码,算是一个比较复杂的类,所以这当中一定有值得我们研究的...

    java8源码-csn-list:ArrayList、LinkedList、Vector、Stack源码分析

    AbstractList 实现 List接口 ArrayList 是一个数组队列,相当于 动态数组。与Java中的数组相比,它的容量能动态增长。不是线程安全的。ArrayList包含了两个重要的对象:elementData(Object[]类型的数组) 和 size ...

    Java集合类中文介绍

    本文首先对Java集合类框架做了简单说明,之后对主要类和为API做了介绍:Collection、List、Set、AbstractCollection、AbstractList、AbstractSet、Iterator、ListIterator。

    AndroidStudio-2021.2.1 lombok

    由于官方目前没有发布最新的lombok支持,故修改了一个供大家来下载。

    Excel 与JSON互转

    说 明:对Excel的旧版和新版(.xls .xlsx)分开处理,以达到兼容 。 文章介绍:https://blog.csdn.net/guzuoi/article/details/81221054

    安卓相册选择库示例

    详情 https://github.com/hubangmao/PhotoSelectLibrary

Global site tag (gtag.js) - Google Analytics