Skip to content

Commit 91eeb6e

Browse files
authored
Merge pull request onlyliuxin#12 from Hipple/master
Master
2 parents f8a1eb7 + d5a0650 commit 91eeb6e

File tree

4 files changed

+77
-3
lines changed

4 files changed

+77
-3
lines changed

group18/1159828430/20170219/.classpath

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,5 @@
22
<classpath>
33
<classpathentry kind="src" path="src"/>
44
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
5-
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
65
<classpathentry kind="output" path="bin"/>
76
</classpath>

group18/1159828430/20170219/src/com/coding/basic/ArrayList.java

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,9 @@ public boolean isEmpty(){
9999
return size == 0;
100100
}
101101

102+
//迭代器
102103
public Iterator iterator(){
103-
return null;
104+
return new ArrayListIterator(this);
104105
}
105106

106107
//动态增加ArrayList大小
@@ -128,6 +129,35 @@ private void fastRemove(int index) {
128129
}
129130
elementData[--size] = null;
130131
}
132+
133+
private class ArrayListIterator implements Iterator{
134+
private ArrayList list = null;
135+
private int cursor = 0;
136+
private int lastRet = -1;
137+
138+
public ArrayListIterator(ArrayList list){
139+
this.list = list;
140+
}
141+
@Override
142+
public boolean hasNext() {
143+
return cursor != list.size;
144+
}
145+
146+
@Override
147+
public Object next() {
148+
lastRet = cursor;
149+
Object o = list.get(lastRet);
150+
cursor ++;
151+
return o;
152+
}
153+
@Override
154+
public void remove() {
155+
list.remove(lastRet);
156+
cursor = lastRet;
157+
lastRet = -1;
158+
}
159+
160+
}
131161

132162
}
133163
class testArrayList{
@@ -138,8 +168,16 @@ public static void main(String[] args) {
138168
}
139169
arrayList.add(5,15);
140170
arrayList.remove(11);
171+
Iterator it = arrayList.iterator();
172+
while(it.hasNext()) {
173+
Integer o = (Integer)it.next();
174+
if(o == 8){
175+
it.remove();
176+
}
177+
}
141178
for (int i = 0; i < arrayList.size(); i++) {
142179
System.out.println("value is "+arrayList.get(i));
143180
}
181+
144182
}
145183
}

group18/1159828430/20170219/src/com/coding/basic/Iterator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@
77
public interface Iterator {
88
public boolean hasNext();
99
public Object next();
10+
public void remove();
1011

1112
}

group18/1159828430/20170219/src/com/coding/basic/LinkedList.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.coding.basic;
22

3+
import java.util.ConcurrentModificationException;
34
import java.util.NoSuchElementException;
45

56
/**
@@ -88,7 +89,7 @@ public Object getFirst() {
8889
}
8990

9091
public Iterator iterator(){
91-
return null;
92+
return new LinkedListIterator();
9293
}
9394

9495
//头部增加节点
@@ -225,6 +226,41 @@ private boolean isPositionIndex(int index) {
225226
return index >= 0 && index <= size;
226227
}
227228

229+
//迭代器
230+
private class LinkedListIterator implements Iterator{
231+
private Node lastReturned = null;
232+
private Node next;
233+
private int nextIndex;
234+
235+
public boolean hasNext() {
236+
return nextIndex < size;
237+
}
238+
239+
public Object next() {
240+
if (!hasNext())
241+
throw new NoSuchElementException();
242+
243+
lastReturned = next;
244+
next = next.next;
245+
nextIndex++;
246+
return lastReturned.data;
247+
}
248+
249+
public void remove() {
250+
if (lastReturned == null)
251+
throw new IllegalStateException();
252+
253+
Node lastNext = lastReturned.next;
254+
unlink(lastReturned);
255+
if (next == lastReturned)
256+
next = lastNext;
257+
else
258+
nextIndex--;
259+
lastReturned = null;
260+
}
261+
}
262+
263+
//节点对象
228264
private static class Node{
229265
Object data;
230266
Node next;

0 commit comments

Comments
 (0)