hrow new NoSuchElementException("Illegal call to previous()"
? ? ? ? ? ? ? ? ? ? ? ? + "iterator is before beginning of list");
? ? ? ? }
? ? ? ? // 返回当前元素的索引
? ? ? ? public int nextIndex() {
? ? ? ? ? ? int result;
? ? ? ? ? ? if (hasNext())
? ? ? ? ? ? ? ? result = nextIndex + 1;
? ? ? ? ? ? else
? ? ? ? ? ? ? ? result = size;// 迭代超出顺序表的最后一个元素时返回顺序表中元素的个数
? ? ? ? ? ? return result;
? ? ? ? }
? ? ? ? // 返回当前元素的前一个元素的索引
? ? ? ? public int previousIndex() {
? ? ? ? ? ? int result;
? ? ? ? ? ? if (hasPrevious())
? ? ? ? ? ? ? ? result = nextIndex - 1;
? ? ? ? ? ? else
? ? ? ? ? ? ? ? result = -1;// 迭代在顺序表的第一个元素时,返回-1
? ? ? ? ? ? return result;
? ? ? ? }
? ? ? ? // 删除当前迭代的元素
? ? ? ? public void remove() {
? ? ? ? ? ? if (isRemoveOrSetLegal) {
? ? ? ? ? ? ? ? isRemoveOrSetLegal = false;
? ? ? ? ? ? ? ? if (lastMove.equals(Move.NEXT)) {
? ? ? ? ? ? ? ? ? ? // next()被调用,但add() remove()没有被调用
? ? ? ? ? ? ? ? ? ? SequenceListWithIterator.this.delete(nextIndex - 1);
? ? ? ? ? ? ? ? ? ? nextIndex--;
? ? ? ? ? ? ? ? } else if (lastMove.equals(Move.PREVIOUS)) {
? ? ? ? ? ? ? ? ? ? SequenceListWithIterator.this.delete(nextIndex);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? } else
? ? ? ? ? ? ? ? throw new IllegalStateException(
? ? ? ? ? ? ? ? ? ? ? ? "Illegal call to remove();"
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? + "next() or previous() was not called, OR add() or remove() called since then");
? ? ? ? }
? ? ? ? // 更改当前迭代的元素
? ? ? ? public void set(T e) {
? ? ? ? ? ? if (isRemoveOrSetLegal) {
? ? ? ? ? ? ? ? if (lastMove.equals(Move.NEXT))
? ? ? ? ? ? ? ? ? ? // 使用内部类机制,内部类里面可以可以直接访问它外部类的底层数据
? ? ? ? ? ? ? ? ? ? elementData[nextIndex - 1] = e;
? ? ? ? ? ? ? ? else {
? ? ? ? ? ? ? ? ? ? assert lastMove.equals(Move.PREVIOUS);
? ? ? ? ? ? ? ? ? ? elementData[nextIndex] = e;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? throw new IllegalStateException(
? ? ? ? ? ? ? ? ? ? ? ? "Illegal call to set();"
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? + "next() or previous() was not called, OR add() or remove() called since then");
? ? ? ? ? ? }
? ? ? ? }
? ? ? ? // 在迭代器的当前元素之前添加一个元素,注意是之前
? ? ? ? public void add(T e) {
? ? ? ? ? ? // set 和 remove 操作只能是在迭代器访问过(跳跃过)某元素之后才能进行
? ? ? ? ? ? isRemoveOrSetLegal = false; // 进行add操作之后,不能立即进行set() 或者 remove()
? ? ? ? ? ? SequenceListWithIterator.this.insert(e, nextIndex++);
? ? ? ? }
? ? }
? ? // 以默认的大小创建顺序表
? ? public SequenceListWithIterator() {
? ? ? ? capacity = DEFAULT_SIZE;
? ? ? ? elementData = new Object[capacity];
? ? }
? ? // 以指定的大小创建顺序表
? ? public SequenceListWithIterator(int initSize) {
? ? ? ? capacity = 1;
? ? ? ? while (capacity < initSize)
? ? ? ? ? ? capacity <<= 1;// 将capacity设置成大于initSize的最小2次方
? ? ? ? elementData = new Object[capacity];
? ? }
? ? public ListIterator getIterator() {
? ? ? ? return new IteratorForSequenceList();
? ? }
? ? // 获取顺序表中当前元素的个数
? ? public int length() {
? ? ? ? return size;
? ? }
? ? // 获取顺序表中索引为 i 处的元素,i表示索引,即以 0 开始
? ? public T get(int i) {
? ? ? ? if (i < 0 || i > size - 1)
? ? ? ? ? ? throw new IndexOutOfBoundsException("顺序表索引越界");
? ? ? ? return (T) elementData[i];
? ? }
? ? // 查看顺序表中指定元素的索引,若未找到,返回-1
? ? public int locate(T element) {
? ? ? ? for (int i = 0; i < size; i++)
? ? ? ? ? ? if (elementData[i].equals(element))
? ? ? ? ? ? ? ? return i;
? ? ? ? return -1;
? ? }
? ? // 在顺序表的指定索引处插入一个元素
? ? public void insert(T element, int index) {
? ? ? ? if (index < 0 || index > size)
? ? ? ? ? ? throw new IndexOutOfBoundsException("顺序表索引越界");
? ? ? ? ensureCapacity(size + 1);// 确保顺序表满时进行扩容,从而能插入元素
? ? ? ? // 将指定索引后的所有元素向后移动一个位置
? ? ? ? // System.arraycopy(elementData, index, elementData, index + 1, size -
? ? ? ? // index);
? ? ? ? for (int i = size; i > index; i--)
? ? ? ? ? ? elementData[i] = elementData[i - 1];
? ? ? ? elementData[index] = element;
? ? ? ? size++;// 顺序表中的元素个数增1
? ? }
? ? private void ensureCapacity(int minCapacity) {
? ? ? ? // 当数组容量已满时,对数组进行扩容。将容量扩展到大于minCapacity的最小2的次方
? ? ? ? if (minCapacity > capacity) {
? ? ? ? ? ? while (capacity < minCapacity)
? ? ? ? ? ? ? ? capacity <<= 1;
? ? ? ? ? ? elementData = Arrays.copyOf(el