Skip to content

Commit 77e6746

Browse files
committed
finish ArrayList\'s iterator method
1 parent 7c83996 commit 77e6746

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

group08/769638826/src/main/java/com/coding/basic/ArrayList.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,15 @@ public int size() {
7676
}
7777

7878
public Iterator iterator() {
79-
return null;
79+
return new ListIterator();
8080
}
8181

8282
private class ListIterator implements Iterator{
83+
int cursor;
84+
8385
@Override
8486
public boolean hasNext() {
85-
return size != 0;
87+
return cursor != size;
8688
}
8789

8890
public void remove(){
@@ -91,12 +93,19 @@ public void remove(){
9193

9294
@Override
9395
public Object next() {
94-
if(!hasNext()){
96+
if(!hasNext()) {
9597
throw new NoSuchElementException();
9698
}
9799

98-
//TODO
99-
return null;
100+
int i = cursor;
101+
if (i >= size)
102+
throw new NoSuchElementException();
103+
104+
Object[] elementData = ArrayList.this.elementData;
105+
106+
cursor = i + 1;
107+
108+
return elementData[i];
100109
}
101110
}
102111
}

0 commit comments

Comments
 (0)