Skip to content

Commit 7ab3c8f

Browse files
authored
Update List.java
Hi Justin, I'm a big fan of your code, but, I think I may have spotted an issue in your add method for ArrayList. When the index of the element that you want to insert is smaller than the size of the list, you shift the array down one spot and insert the value at the desired position. However, I think you forgot to increase the size of the list after that, which may cause some problems when iterating through the arrayList.
1 parent 77a4d0d commit 7ab3c8f

File tree

1 file changed

+1
-0
lines changed
  • src/com/jwetherell/algorithms/data_structures

1 file changed

+1
-0
lines changed

src/com/jwetherell/algorithms/data_structures/List.java

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public boolean add(int index, T value) {
4646
// Shift the array down one spot
4747
System.arraycopy(array, index, array, index+1, size - index);
4848
array[index] = value;
49+
size++;
4950
}
5051
return true;
5152
}

0 commit comments

Comments
 (0)