File tree Expand file tree Collapse file tree 2 files changed +42
-5
lines changed
main/java/org/bk/ass/collection
test/java/org/bk/ass/collection Expand file tree Collapse file tree 2 files changed +42
-5
lines changed Original file line number Diff line number Diff line change 1
1
package org .bk .ass .collection ;
2
2
3
- import java .util .AbstractCollection ;
4
- import java .util .Collection ;
5
- import java .util .Iterator ;
6
- import java .util .NoSuchElementException ;
3
+ import java .util .*;
7
4
8
5
import static java .lang .Math .max ;
9
6
import static java .lang .Math .min ;
@@ -107,7 +104,7 @@ public Iterator<T> iterator() {
107
104
108
105
@ Override
109
106
public Object [] toArray () {
110
- return items ;
107
+ return Arrays . copyOf ( items , size ) ;
111
108
}
112
109
113
110
@ Override
@@ -135,6 +132,11 @@ public void clear() {
135
132
private class UnorderedListIterator implements Iterator <T > {
136
133
private int index ;
137
134
135
+ @ Override
136
+ public void remove () {
137
+ UnorderedCollection .this .removeAt (index - 1 );
138
+ }
139
+
138
140
@ Override
139
141
public boolean hasNext () {
140
142
return index < size ;
Original file line number Diff line number Diff line change 3
3
import org .junit .jupiter .api .Test ;
4
4
5
5
import java .util .Arrays ;
6
+ import java .util .Iterator ;
6
7
7
8
import static org .assertj .core .api .Assertions .assertThat ;
8
9
@@ -84,4 +85,38 @@ public void shouldRemoveElements() {
84
85
// THEN
85
86
assertThat (sut ).containsExactly ("test2" );
86
87
}
88
+
89
+ @ Test
90
+ void shouldRemoveInIterator () {
91
+ // GIVEN
92
+ String toRemove = "test" ;
93
+ sut .add (toRemove );
94
+
95
+ Iterator <Object > iterator = sut .iterator ();
96
+ iterator .next ();
97
+
98
+ // WHEN
99
+ iterator .remove ();
100
+
101
+ // THEN
102
+ assertThat (sut ).isEmpty ();
103
+ }
104
+
105
+ @ Test
106
+ void shouldRemoveCorrectItemInIterator () {
107
+ // GIVEN
108
+ String toRemove = "test" ;
109
+ sut .add (toRemove );
110
+ String expectedRemainingItem = "test2" ;
111
+ sut .add (expectedRemainingItem );
112
+
113
+ Iterator <Object > iterator = sut .iterator ();
114
+ iterator .next ();
115
+
116
+ // WHEN
117
+ iterator .remove ();
118
+
119
+ // THEN
120
+ assertThat (sut ).containsExactly (expectedRemainingItem );
121
+ }
87
122
}
You can’t perform that action at this time.
0 commit comments