From d5a0650692c48344bcaa10fff1a9c90aba506a48 Mon Sep 17 00:00:00 2001 From: Hipple Date: Sun, 26 Feb 2017 13:41:34 +0800 Subject: [PATCH] Interator author Hipple --- group18/1159828430/20170219/.classpath | 1 - .../src/com/coding/basic/ArrayList.java | 40 ++++++++++++++++++- .../src/com/coding/basic/Iterator.java | 1 + .../src/com/coding/basic/LinkedList.java | 38 +++++++++++++++++- 4 files changed, 77 insertions(+), 3 deletions(-) diff --git a/group18/1159828430/20170219/.classpath b/group18/1159828430/20170219/.classpath index 2d7497573f..d171cd4c12 100644 --- a/group18/1159828430/20170219/.classpath +++ b/group18/1159828430/20170219/.classpath @@ -2,6 +2,5 @@ - diff --git a/group18/1159828430/20170219/src/com/coding/basic/ArrayList.java b/group18/1159828430/20170219/src/com/coding/basic/ArrayList.java index c8db730110..cb29afc276 100644 --- a/group18/1159828430/20170219/src/com/coding/basic/ArrayList.java +++ b/group18/1159828430/20170219/src/com/coding/basic/ArrayList.java @@ -99,8 +99,9 @@ public boolean isEmpty(){ return size == 0; } + //迭代器 public Iterator iterator(){ - return null; + return new ArrayListIterator(this); } //动态增加ArrayList大小 @@ -128,6 +129,35 @@ private void fastRemove(int index) { } elementData[--size] = null; } + + private class ArrayListIterator implements Iterator{ + private ArrayList list = null; + private int cursor = 0; + private int lastRet = -1; + + public ArrayListIterator(ArrayList list){ + this.list = list; + } + @Override + public boolean hasNext() { + return cursor != list.size; + } + + @Override + public Object next() { + lastRet = cursor; + Object o = list.get(lastRet); + cursor ++; + return o; + } + @Override + public void remove() { + list.remove(lastRet); + cursor = lastRet; + lastRet = -1; + } + + } } class testArrayList{ @@ -138,8 +168,16 @@ public static void main(String[] args) { } arrayList.add(5,15); arrayList.remove(11); + Iterator it = arrayList.iterator(); + while(it.hasNext()) { + Integer o = (Integer)it.next(); + if(o == 8){ + it.remove(); + } + } for (int i = 0; i < arrayList.size(); i++) { System.out.println("value is "+arrayList.get(i)); } + } } diff --git a/group18/1159828430/20170219/src/com/coding/basic/Iterator.java b/group18/1159828430/20170219/src/com/coding/basic/Iterator.java index 0ce3f762ba..b47b93b41a 100644 --- a/group18/1159828430/20170219/src/com/coding/basic/Iterator.java +++ b/group18/1159828430/20170219/src/com/coding/basic/Iterator.java @@ -7,5 +7,6 @@ public interface Iterator { public boolean hasNext(); public Object next(); + public void remove(); } \ No newline at end of file diff --git a/group18/1159828430/20170219/src/com/coding/basic/LinkedList.java b/group18/1159828430/20170219/src/com/coding/basic/LinkedList.java index 433715d5ad..4586f0549d 100644 --- a/group18/1159828430/20170219/src/com/coding/basic/LinkedList.java +++ b/group18/1159828430/20170219/src/com/coding/basic/LinkedList.java @@ -1,5 +1,6 @@ package com.coding.basic; +import java.util.ConcurrentModificationException; import java.util.NoSuchElementException; /** @@ -88,7 +89,7 @@ public Object getFirst() { } public Iterator iterator(){ - return null; + return new LinkedListIterator(); } //头部增加节点 @@ -225,6 +226,41 @@ private boolean isPositionIndex(int index) { return index >= 0 && index <= size; } + //迭代器 + private class LinkedListIterator implements Iterator{ + private Node lastReturned = null; + private Node next; + private int nextIndex; + + public boolean hasNext() { + return nextIndex < size; + } + + public Object next() { + if (!hasNext()) + throw new NoSuchElementException(); + + lastReturned = next; + next = next.next; + nextIndex++; + return lastReturned.data; + } + + public void remove() { + if (lastReturned == null) + throw new IllegalStateException(); + + Node lastNext = lastReturned.next; + unlink(lastReturned); + if (next == lastReturned) + next = lastNext; + else + nextIndex--; + lastReturned = null; + } + } + + //节点对象 private static class Node{ Object data; Node next;