`
默默的小熊
  • 浏览: 227486 次
社区版块
存档分类
最新评论

AbstractList

 
阅读更多

 

public abstract class AbstractList<E> extends AbstractCollection<E> implements
		List<E> {
	protected AbstractList() {
	}

	public boolean add(E o) {
		add(size(), o);
		return true;
	}

	abstract public E get(int index);

	public E set(int index, E element) {
		throw new UnsupportedOperationException();
	}

	public void add(int index, E element) {
		throw new UnsupportedOperationException();
	}

	public E remove(int index) {
		throw new UnsupportedOperationException();
	}

	public int indexOf(Object o) {
		ListIterator<E> e = listIterator();
		if (o == null) {
			while (e.hasNext())
				if (e.next() == null)
					return e.previousIndex();
		} else {
			while (e.hasNext())
				if (o.equals(e.next()))
					return e.previousIndex();
		}
		return -1;
	}

	public int lastIndexOf(Object o) {
		ListIterator<E> e = listIterator(size());
		if (o == null) {
			while (e.hasPrevious())
				if (e.previous() == null)
					return e.nextIndex();
		} else {
			while (e.hasPrevious())
				if (o.equals(e.previous()))
					return e.nextIndex();
		}
		return -1;
	}

	public void clear() {
		removeRange(0, size());
	}

	public boolean addAll(int index, Collection<? extends E> c) {
		boolean modified = false;
		Iterator<? extends E> e = c.iterator();
		while (e.hasNext()) {
			add(index++, e.next());
			modified = true;
		}
		return modified;
	}

	public Iterator<E> iterator() {
		return new Itr();
	}

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

	public ListIterator<E> listIterator(final int index) {
		if (index < 0 || index > size())
			throw new IndexOutOfBoundsException("Index: " + index);

		return new ListItr(index);
	}

	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 {
				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 o) {
			if (lastRet == -1)
				throw new IllegalStateException();
			checkForComodification();

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

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

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

	public List<E> subList(int fromIndex, int toIndex) {
		return (this instanceof RandomAccess ? new RandomAccessSubList<E>(this,
				fromIndex, toIndex) : new SubList<E>(this, fromIndex, toIndex));
	}

	public boolean equals(Object o) {
		if (o == this)
			return true;
		if (!(o instanceof List))
			return false;

		ListIterator<E> e1 = listIterator();
		ListIterator e2 = ((List) o).listIterator();
		while (e1.hasNext() && e2.hasNext()) {
			E o1 = e1.next();
			Object o2 = e2.next();
			if (!(o1 == null ? o2 == null : o1.equals(o2)))
				return false;
		}
		return !(e1.hasNext() || e2.hasNext());
	}

	public int hashCode() {
		int hashCode = 1;
		Iterator<E> i = iterator();
		while (i.hasNext()) {
			E obj = i.next();
			hashCode = 31 * hashCode + (obj == null ? 0 : obj.hashCode());
		}
		return hashCode;
	}

	protected void removeRange(int fromIndex, int toIndex) {
		ListIterator<E> it = listIterator(fromIndex);
		for (int i = 0, n = toIndex - fromIndex; i < n; i++) {
			it.next();
			it.remove();
		}
	}

	protected transient int modCount = 0;
}

class SubList<E> extends AbstractList<E> {
	private AbstractList<E> l;
	private int offset;
	private int size;
	private int expectedModCount;

	SubList(AbstractList<E> list, int fromIndex, int toIndex) {
		if (fromIndex < 0)
			throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
		if (toIndex > list.size())
			throw new IndexOutOfBoundsException("toIndex = " + toIndex);
		if (fromIndex > toIndex)
			throw new IllegalArgumentException("fromIndex(" + fromIndex
					+ ") > toIndex(" + toIndex + ")");
		l = list;
		offset = fromIndex;
		size = toIndex - fromIndex;
		expectedModCount = l.modCount;
	}

	public E set(int index, E element) {
		rangeCheck(index);
		checkForComodification();
		return l.set(index + offset, element);
	}

	public E get(int index) {
		rangeCheck(index);
		checkForComodification();
		return l.get(index + offset);
	}

	public int size() {
		checkForComodification();
		return size;
	}

	public void add(int index, E element) {
		if (index < 0 || index > size)
			throw new IndexOutOfBoundsException();
		checkForComodification();
		l.add(index + offset, element);
		expectedModCount = l.modCount;
		size++;
		modCount++;
	}

	public E remove(int index) {
		rangeCheck(index);
		checkForComodification();
		E result = l.remove(index + offset);
		expectedModCount = l.modCount;
		size--;
		modCount++;
		return result;
	}

	protected void removeRange(int fromIndex, int toIndex) {
		checkForComodification();
		l.removeRange(fromIndex + offset, toIndex + offset);
		expectedModCount = l.modCount;
		size -= (toIndex - fromIndex);
		modCount++;
	}

	public boolean addAll(Collection<? extends E> c) {
		return addAll(size, c);
	}

	public boolean addAll(int index, Collection<? extends E> c) {
		if (index < 0 || index > size)
			throw new IndexOutOfBoundsException("Index: " + index + ", Size: "
					+ size);
		int cSize = c.size();
		if (cSize == 0)
			return false;

		checkForComodification();
		l.addAll(offset + index, c);
		expectedModCount = l.modCount;
		size += cSize;
		modCount++;
		return true;
	}

	public Iterator<E> iterator() {
		return listIterator();
	}

	public ListIterator<E> listIterator(final int index) {
		checkForComodification();
		if (index < 0 || index > size)
			throw new IndexOutOfBoundsException("Index: " + index + ", Size: "
					+ size);

		return new ListIterator<E>() {
			private ListIterator<E> i = l.listIterator(index + offset);

			public boolean hasNext() {
				return nextIndex() < size;
			}

			public E next() {
				if (hasNext())
					return i.next();
				else
					throw new NoSuchElementException();
			}

			public boolean hasPrevious() {
				return previousIndex() >= 0;
			}

			public E previous() {
				if (hasPrevious())
					return i.previous();
				else
					throw new NoSuchElementException();
			}

			public int nextIndex() {
				return i.nextIndex() - offset;
			}

			public int previousIndex() {
				return i.previousIndex() - offset;
			}

			public void remove() {
				i.remove();
				expectedModCount = l.modCount;
				size--;
				modCount++;
			}

			public void set(E o) {
				i.set(o);
			}

			public void add(E o) {
				i.add(o);
				expectedModCount = l.modCount;
				size++;
				modCount++;
			}
		};
	}

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

	private void rangeCheck(int index) {
		if (index < 0 || index >= size)
			throw new IndexOutOfBoundsException("Index: " + index + ",Size: "
					+ size);
	}

	private void checkForComodification() {
		if (l.modCount != expectedModCount)
			throw new ConcurrentModificationException();
	}
}

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<E>(this, fromIndex, toIndex);
	}
}
 

 

分享到:
评论

相关推荐

    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。

    APKOB实用型反编译软件

    使用非常简单的,非常实用的,安卓apk反编译软件

    MySql转Java实体类

    将一个 .sql文件中的表转换为对应的Java实体类

    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