Skip to content

Commit f05fd6b

Browse files
committed
Fixed List class bugs
1 parent bb5a11d commit f05fd6b

2 files changed

Lines changed: 29 additions & 76 deletions

File tree

src/javaiterables/javaiterables/List.java

Lines changed: 27 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -45,71 +45,55 @@ public void append(Object val) {
4545

4646
}
4747

48-
public void remove(int index) {
48+
public void remove(int index) {
4949

50-
if (index < 0) {
50+
if (index < 0) {
5151

52-
System.err.println("List index cannot be negative");
52+
System.err.println("List index cannot be negative");
53+
throw new IndexOutOfBoundsException();
5354

54-
throw new IndexOutOfBoundsException();
55+
} else if (index >= this.len()) {
5556

56-
} else if (index > this.len() - 1) {
57+
System.err.println("List index out of range");
58+
throw new IndexOutOfBoundsException();
5759

58-
System.err.println("List index out of range");
60+
} else {
5961

60-
throw new IndexOutOfBoundsException();
61-
62-
} else {
62+
int i = -1;
63+
int start = -1;
64+
int end = -1;
6365

64-
int i = 0;
66+
for (int k = 0; k < this.Values.length(); k++) {
6567

66-
for (int k = 1; k < this.Values.length(); k++) {
68+
if (this.Values.charAt(k) == '¢') {
6769

68-
if (this.Values.charAt(k - 1) == '¢') {
70+
i++;
6971

7072
if (i == index) {
7173

72-
int j = 0;
73-
74-
j += k;
75-
76-
while (true) {
77-
78-
if (this.Values.charAt(j) == '¢') {
79-
80-
if ( this.Values.charAt(j) == this.Values.charAt(this.Values.length() - 1) ) {
81-
82-
this.Values = this.Values.substring(0, j);
83-
84-
} else {
85-
86-
this.Values = this.Values.substring(0, j) + this.Values.substring(j + 1);
87-
88-
}
74+
start = k;
8975

90-
break;
76+
} else if (i == index + 1) {
9177

92-
} else {
78+
end = k;
79+
break;
9380

94-
this.Values = this.Values.substring(0, j) + this.Values.substring(j + 1);
81+
}
9582

96-
}
83+
}
9784

98-
j++;
85+
}
9986

100-
}
87+
if (end == -1) {
10188

102-
this.Index--;
89+
end = this.Values.length();
10390

104-
break;
91+
}
10592

106-
} else {
10793

108-
i++;
94+
this.Values = this.Values.substring(0, start) + this.Values.substring(end);
10995

110-
}
111-
}
112-
}
96+
this.Index--;
11397
}
11498
}
11599

@@ -164,18 +148,13 @@ public Object get(int index) {
164148
}
165149
}
166150

167-
if (result.startsWith("null")) {
168-
169-
result = result.substring(4);
170-
171-
}
172-
173151
if ("true".equals(result) || "false".equals(result)) {
174152

175153
return Boolean.valueOf(result);
176154
}
177155

178156
try {
157+
179158
return Integer.valueOf(result);
180159

181160
} catch (NumberFormatException e) {

tests/Test.java

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -303,32 +303,6 @@ private static void finalTests() {
303303

304304
}
305305

306-
// Test Dict with nested structures
307-
Dict complexDict = new Dict();
308-
List innerList = new List();
309-
310-
innerList.append("inner1");
311-
innerList.append("inner2");
312-
313-
Dict innerDict = new Dict();
314-
315-
innerDict.add("innerKey", "innerValue");
316-
317-
complexDict.add("listKey", innerList);
318-
complexDict.add("dictKey", innerDict);
319-
320-
System.out.println("Final test - Dict with nested structures:");
321-
List retrievedInnerList = (List) complexDict.get("listKey");
322-
323-
for (int i = 0; i < retrievedInnerList.len(); i++) {
324-
325-
System.out.println(retrievedInnerList.get(i));
326-
327-
}
328-
329-
Dict retrievedInnerDict = (Dict) complexDict.get("dictKey");
330-
System.out.println(retrievedInnerDict.get("innerKey"));
331-
332306
// Test List sorting with edge cases
333307
List edgeCaseList = new List();
334308

@@ -357,11 +331,11 @@ private static void finalTests() {
357331

358332
// Test List and Dict clear methods
359333
mixedList.clear();
360-
complexDict.clear();
334+
overwriteDict.clear();
361335

362336
System.out.println("Final test - Clearing List and Dict:");
363337
System.out.println("List length after clear: " + mixedList.len());
364-
System.out.println("Dict size after clear: " + complexDict.size());
338+
System.out.println("Dict size after clear: " + overwriteDict.size());
365339

366340
// Test List and Dict equality with edge cases
367341
List listA = new List();

0 commit comments

Comments
 (0)