Skip to content

Commit a8182a1

Browse files
committed
Iterable interface implement in ArrayListCustom
1 parent 791e0e9 commit a8182a1

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/Lists/ArrayListCustom.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import java.util.function.Consumer;
55
import java.util.function.Predicate;
66

7-
public class ArrayListCustom<T> implements Cloneable {
7+
public class ArrayListCustom<T> implements Cloneable,Iterable<T> {
88

99
private final int DEFAULT_CAPACITY = 7;
1010
private int size = 0;
@@ -114,7 +114,7 @@ public String toString() {
114114
}
115115

116116
// forEach method takes Consumer<T>
117-
public void forEach(Consumer<T> o) {
117+
public void forEach(Consumer<? super T> o) {
118118
Objects.requireNonNull(o);
119119
for (int i = 0; i < size; i++) {
120120
o.accept((T) arr[i]);
@@ -128,6 +128,11 @@ public ArrayListCustom<T> filter(Predicate<T> o) {
128128
return newList;
129129
}
130130

131+
@Override
132+
public Iterator<T> iterator() {
133+
return new ArrayListItr<>();
134+
}
135+
131136
private class ArrayListItr<T> implements Iterator<T> {
132137
int cursor = 0;
133138

0 commit comments

Comments
 (0)