An ordered, finite, mutable data structure
What about sort?
How do we do lists?
create list of a specific size, append, prepend, remove, insert, getLength, search, sort
Let’s implement a list interface.
Let’s start with the basic methods:
get(int index)length() returns how many items in the listset(int index, T value) – changes the value of item at index (return a bollean if value was set successfully)Here’s our initial generic interface:
List interface, we will start by implementing our List with an arrayArrayList and we will use the implements keyword:Here’s how we are setting up the private instance variable: private T[] data;
Constructor:
We will create a generic ArrayList that implements List
public class ArrayList<T> implements List<T> {
private T[] data;
private int size = 10;
private int length = 0;
@SuppressWarnings("unchecked")
public ArrayList() {
data = (T[]) new Object[size];
}
@Override
public int length() {
return length;
}
@Override
public void append(T value) {
// TODO: implement
}
@Override
public T get(int index) {
// TODO: implement
}
@Override
public void set(int index, T value) {
// TODO: implement
}
}Submit your List.java and ArrayList.java files to gradescope.
What happens when length is the same as size?
Submit your List.java and ArrayList.java files to gradescope making sure your array implementation can do multiple append calls (your implementation deals with array expansion)
The arrayExpand process is \(O(n)\) (gotta copy the array!) BUT it doesn’t run every time, so how do we handle the affect on append?
Best case: \(O(1)\) Worst case: \(O(n)\)
What we often care about is not the runtime of myList.append(4); but instead:
double array vs. increase array size by 10 plots
One way to think about it:
Let’s add prepend and insert to our interface.
When implementing ArrayList insert and prepend, we need to shift values in our array to the right
shiftRightSubmit your List.java and ArrayList.java files to gradescope making sure your array implementation contains methods prepend and insert.
T pop(int index) to our List interfaceWe need to implement shiftLeft in ArrrayList
public class UseList {
public static void main(String[] args) {
List<String> fruitList = new ArrayList<>();
System.out.println(fruitList.length()); // 0
System.out.println(fruitList);
fruitList.append("apple");
fruitList.append("pear");
fruitList.append("banana");
fruitList.prepend("kiwi");
fruitList.prepend("tomato");
fruitList.insert(2, "pineapple");
System.out.println(fruitList);
System.out.println(fruitList.pop(1));
System.out.println(fruitList);
}
}Submit your List.java and ArrayList.java files to gradescope making sure your array implementation contains the pop method.
ArrayList implementationpublic class ArrayList<T> implements List<T> {
private T[] data;
private int size = 10;
private int length = 0;
@SuppressWarnings("unchecked")
public ArrayList() {
data = (T[]) new Object[size];
}
@Override
public int length() {
return length;
}
@Override
public void append(T value) {
if (length == size) {
expandArray();
}
data[length] = value;
length++;
}
@Override
public void insert(int index, T value) {
if (index < 0 || index > length) throw new RuntimeException("index our of range");
shiftRight(index);
data[index] = value;
}
@Override
public void prepend(T value) {
shiftRight(0); // need to shift all the other values
data[0] = value;
}
@Override
public T pop(int index) {
if (index < 0 || index >= length) throw new RuntimeException("index out of range");
T out = data[index];
shiftLeft(index);
return out;
}
private void shiftRight(int startIndex) {
length++;
if (length == size) expandArray();
for (int i = length-1; i > startIndex; i--) data[i] = data[i-1];
}
private void shiftLeft(int startIndex) {
for (int i = startIndex; i < length; i++) data[i] = data[i+1];
length--;
}
@SuppressWarnings("unchecked")
private void expandArray() {
size *= 2;
T[] newData = (T[]) new Object[size];
for (int i = 0; i < length; i++) newData[i] = data[i];
data = newData;
}
@Override
public T get(int index) {
if (index < 0 || index >= length) throw new RuntimeException("index out of range");
return data[index];
}
@Override
public void set(int index, T value) {
if (index < 0 || index >= length) throw new RuntimeException("index out of range");
data[length] = value;
}
@Override
public String toString() {
String out = "";
for (int i=0; i < length; i++) out += data[i] + " ";
return out.trim();
}
}
| Front of List | Middle of List | End of List | |
|---|---|---|---|
| get | O(1) | O(1) | O(1) |
| set | O(1) | O(1) | O(1) |
| insert | O(n) | O(n) | O(1) |
| append | O(n) | O(n) | O(1) |
| prepend | O(n) | O(n) | O(n) |
| pop | O(n) | O(n) | O(1) |
You have 10 minutes to complete the quiz