List
PerformanceGradescope HW02 Link Later Today
Attendance and COVID
ArraySimpleList
ArraySimpleList
PerformanceArraySimpleList
LinkedSimpleList
ImplementationArraySimpleList
List
ADT to represent lists of elements
size()
isEmpty()
get(i)
set(i, y)
add(i, y)
remove(i)
Discussed implementing List
using an array to store contents
ArraySimpleList.java
List
as ArrayStarting from $1, 2, 4, 5$, how to…
…add(2, 3)
?
…remove(0)
?
Does not compile:
E[] contents;
Compiles, but compiler complains
Object[] contents
...
E element = (E) contents[i];
Compiles, no complaints
Object[] contents
...
@SuppressWarnings("unchecked")
E element = (E) contents[i];
ArraySimpleList
PerformanceAre we happy with this performance?
ArraySimpleList
Every time we add an element, we copy entire array contents!
Can we do better?
Double the array size whenever we increase capacity
Why does this help? How much does it help?
Before:
private void increaseCapacity() {
Object[] bigContents = new Object[capacity + 1];
for (int i = 0; i < capacity; ++i) {
bigContents[i] = contents[i];
}
contents = bigContents;
capacity = capacity + 1;
}
After:
private void increaseCapacity() {
Object[] bigContents = new Object[2 * capacity];
for (int i = 0; i < capacity; ++i) {
bigContents[i] = contents[i];
}
contents = bigContents;
capacity = 2 * capacity;
}
Second implementation built a list of size 5 million faster than first implementation built a list of size 10 thousand!
Why is performance so much better?