Skip to content

Commit bc2955e

Browse files
committed
bug fix for ArrayList
1 parent 96719c0 commit bc2955e

File tree

3 files changed

+11
-188
lines changed

3 files changed

+11
-188
lines changed

group20/1107837739/1107837739Learning/src/org/korben/coding/basic/list/KArrayList.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public void clear() {
7474
@Override
7575
@SuppressWarnings("unchecked")
7676
public T get(int index) {
77-
if (index < -1 || index >= size) {
77+
if (index < 0 || index >= size) {
7878
throw new IndexOutOfBoundsException();
7979
}
8080

@@ -83,7 +83,7 @@ public T get(int index) {
8383

8484
@Override
8585
public T set(int index, T element) {
86-
if (index < -1 || index >= size) {
86+
if (index < 0 || index >= size) {
8787
throw new IndexOutOfBoundsException();
8888
}
8989

@@ -94,7 +94,7 @@ public T set(int index, T element) {
9494

9595
@Override
9696
public void add(int index, T element) {
97-
if (index < -1 || index > size) {
97+
if (index < 0 || index > size) {
9898
throw new IndexOutOfBoundsException();
9999
}
100100

@@ -109,7 +109,7 @@ public void add(int index, T element) {
109109
@Override
110110
@SuppressWarnings("unchecked")
111111
public T remove(int index) {
112-
if (index < -1 || index >= size) {
112+
if (index < 0 || index >= size) {
113113
throw new IndexOutOfBoundsException();
114114
}
115115

@@ -132,7 +132,7 @@ public int indexOf(T o) {
132132

133133
@Override
134134
public KIterator<T> iterator() {
135-
return new ArrayListIterator(this);
135+
return new ArrayListIterator();
136136
}
137137

138138
private void ensureCapacity(int minCapacity) {
@@ -147,21 +147,19 @@ private void ensureCapacity(int minCapacity) {
147147

148148
private class ArrayListIterator implements KIterator<T> {
149149
private int position;
150-
private KArrayList<T> list;
151150

152-
ArrayListIterator(KArrayList<T> list) {
153-
this.list = list;
151+
ArrayListIterator() {
154152
}
155153

156154
@Override
157155
public boolean hasNext() {
158-
return position < list.size();
156+
return position < size();
159157
}
160158

161159
@Override
162160
public T next() {
163161
if (hasNext()) {
164-
return list.get(position++);
162+
return get(position++);
165163
}
166164
return null;
167165
}

group20/1107837739/1107837739Learning/src/org/korben/coding/basic/list/KDoubleLinkedList.java

Lines changed: 0 additions & 175 deletions
This file was deleted.

group20/1107837739/1107837739Learning/src/org/korben/coding/basic/list/KLinkedList.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ public int indexOf(T o) {
150150

151151
@Override
152152
public KIterator<T> iterator() {
153-
return new Iterator(this);
153+
return new Iterator();
154154
}
155155

156156
private Node<T> getNode(int index) {
@@ -474,8 +474,8 @@ private static class Node<T> {
474474
private class Iterator implements KIterator<T> {
475475
private Node<T> node;
476476

477-
Iterator(KLinkedList<T> list) {
478-
this.node = list.head;
477+
Iterator() {
478+
this.node = head;
479479
}
480480

481481
@Override

0 commit comments

Comments
 (0)