List
PerformanceVirtual office hours today (after class)
LinkedSimpleList
and Performance ComparisonExample workflow in Implementing Interfaces in Java
Estimates: software engineers spend 75–90% of their time debugging!
Debugging is an essential part of the development process:
Considered ArraySimpleList
implementation of SimpleList
increaseCapacity
methodBefore:
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;
}
public void add(int i, E x) {
if (i > size || i < 0) {
throw new IndexOutOfBoundsException();
}
if (size == capacity) {
increaseCapacity();
}
++size;
Object cur = x;
for (int j = i; j < size; ++j) {
Object next = contents[j];
contents[j] = cur;
cur = next;
}
}
Performance of adding at index 0
vs size
?
How fast is building an ArraySimpleList
by repeatedly adding at index 0
?
public void add(int i, E x) {
if (i > size || i < 0) {
throw new IndexOutOfBoundsException();
}
if (size == capacity) {
increaseCapacity();
}
++size;
Object cur = x;
for (int j = i; j < size; ++j) {
Object next = contents[j];
contents[j] = cur;
cur = next;
}
}
LinkedSimpleList
ImplementationList
ImplementationRecall the “linked list” data structure
List
as Linked Listget(i)
?add(i, y)
?remove(i, y)
?Why should we expect to see this trend?
Is trend what you’d expect? Why or why not?
Question. Given a List, how can you simulate a Deque
?
addFirst
?addLast
?removeFirst
?public class ASLDeque<E> implements SimpleDeque<E> {
private SimpleList<E> list = new ArraySimpleList<E>();
public int size() { return list.size(); }
public boolean isEmpty() { return list.isEmpty(); }
public void addFirst(E x) { list.add(0, x); }
public E removeFirst() { return list.remove(0); }
public E peekFirst() { return list.get(0); }
public void addLast(E x) { list.add(list.size(), x); }
public E removeLast() { return list.remove(list.size() - 1); }
public E peekLast() { return list.get(list.size() - 1); }
}
If we can always use a List, why would we ever want just a stack/queue/deque?