File tree Expand file tree Collapse file tree 4 files changed +77
-3
lines changed
group18/1159828430/20170219 Expand file tree Collapse file tree 4 files changed +77
-3
lines changed Original file line number Diff line number Diff line change 2
2
<classpath >
3
3
<classpathentry kind =" src" path =" src" />
4
4
<classpathentry kind =" con" path =" org.eclipse.jdt.launching.JRE_CONTAINER" />
5
- <classpathentry kind =" con" path =" org.eclipse.jdt.junit.JUNIT_CONTAINER/4" />
6
5
<classpathentry kind =" output" path =" bin" />
7
6
</classpath >
Original file line number Diff line number Diff line change @@ -99,8 +99,9 @@ public boolean isEmpty(){
99
99
return size == 0 ;
100
100
}
101
101
102
+ //迭代器
102
103
public Iterator iterator (){
103
- return null ;
104
+ return new ArrayListIterator ( this ) ;
104
105
}
105
106
106
107
//动态增加ArrayList大小
@@ -128,6 +129,35 @@ private void fastRemove(int index) {
128
129
}
129
130
elementData [--size ] = null ;
130
131
}
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
+ }
131
161
132
162
}
133
163
class testArrayList {
@@ -138,8 +168,16 @@ public static void main(String[] args) {
138
168
}
139
169
arrayList .add (5 ,15 );
140
170
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
+ }
141
178
for (int i = 0 ; i < arrayList .size (); i ++) {
142
179
System .out .println ("value is " +arrayList .get (i ));
143
180
}
181
+
144
182
}
145
183
}
Original file line number Diff line number Diff line change 7
7
public interface Iterator {
8
8
public boolean hasNext ();
9
9
public Object next ();
10
+ public void remove ();
10
11
11
12
}
Original file line number Diff line number Diff line change 1
1
package com .coding .basic ;
2
2
3
+ import java .util .ConcurrentModificationException ;
3
4
import java .util .NoSuchElementException ;
4
5
5
6
/**
@@ -88,7 +89,7 @@ public Object getFirst() {
88
89
}
89
90
90
91
public Iterator iterator (){
91
- return null ;
92
+ return new LinkedListIterator () ;
92
93
}
93
94
94
95
//头部增加节点
@@ -225,6 +226,41 @@ private boolean isPositionIndex(int index) {
225
226
return index >= 0 && index <= size ;
226
227
}
227
228
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
+ //节点对象
228
264
private static class Node {
229
265
Object data ;
230
266
Node next ;
You can’t perform that action at this time.
0 commit comments