Skip to main content

cs2381 Notes: 12 Array List

··2 mins

Midterm

The midterm exam will be in Lecture next Monday, October 2nd.

Arrays vs Cons Lists

Operation List Array
Get first item O(1) O(1)
Get item by index i O(n) O(1)
Set item at index i O(n) O(1)
Add item to front O(1) O(n)
Add item to back O(n) O(n) (including resize)
Insert item after index ii O(n) O(n) (including resize)

ArrayList

We’ve seen arrays and the idea of a wrapper object.

Let’s add one more method:

    void append(T vv) {
        int ii = length();
        resize(ii + 1);
        set(ii, vv);
    }

Now we can fill an Array with items like this:

    var xs = new ArrayWrap<Integer>();
    for (int ii = 0; ii < 10; ++ii) {
       xs.append(vv); 
    }

Problem: What is the asymptotic complexity of that loop?

  • What’s the complexity of xs.append?
  • And we do that n times.

Counting inserts:

  • 1 for first item.
  • 1 for second, plus copy 1 = 2
  • 1 + copy 2
  • 1 + copy 3
  • 1 + copy (n-1)
  • ~ O(n^2)

We can do better than that. Let’s write an ArrayList.

Trick:

  • Add a length field, allowing the length of the ArrayList to be shorter than the length of the backing array.
  • Grow by doubling the length of the backing array.

Now let’s try counting inserts.

  • 1 for first item (cap = 1)

  • 1 + 1 for second (cap = 2)

  • 1 + 2 for third (but now cap = 4)

  • just one for 4th

  • = 7

  • 1 + 4 for fifth (cap = 8)

  • just 3 for next 3

  • = 15 for 8th

  • 1 + 8 for 9th (cap = 16)

  • just 7 for next 7

  • = 31 for 16th

  • = 63 for 32

  • = 127 for 64

  • = 2*n-1 for n

  • ~ O(n) to insert n

Let’s build (or at least describe) some more methods for ConsList and ArrayList:

  • Insert after index
  • Delete item
  • Concatenate
  • Split into two
  • What if we don’t want to destroy the old one?