---
# System prepended metadata

title: Lecture 11 Setup/Prep
tags: [Setup-Summer21]

---

---
tags: Setup-Summer21

---

# Lecture 11 Setup/Prep

Last lecture, we introduced *arrays*, a built-in data structure that sets aside a contiguous chunk of memory slots for storing a sequence of items in order. Arrays are particularly good for data that you want to access by index or position (such as "first", "second", etc). In particular, the `get` operation (by position) is constant-time on arrays.

One downside to arrays is that adding an element to the front of the list is NOT constant time: this is a linear-time operation because we have to shift all of the elements down one space in the array (so that the `get` operation would continue to work).

Another downside is that arrays have a fixed size. What if we need to add more elements than there are slots?

In this lecture, we'll talk about how to handle both issues. 

## Prep

Here's the [starter code for class](https://brown-cs18-master.github.io/content/lectures/11dynarrays/lec11init.zip).

The starter code has the following basic `ArrayBasedList` class, similar to what we set up last time, but containing strings rather than integers. We'll also take the max size as a constructor parameter rather than fix the value.

```=java
public class ArrayBasedList {
    int maxSize; // will take this as a constructor input
    String[] contents = null;
    int end = 0; //  the next open space in the array
    
    ArrayBasedList(int capacity) {
        this.maxSize = capacity;
        this.contents = new String[maxSize];
    }

    // add given item to the end of the array/list
    ArrayBasedList addLast(String newelt) {
        contents[this.end] = newelt;
        this.end = this.end + 1;
        return this;
    }
}
```

Imagine that we ran the following code:

```=java
ArrayBasedList ourTAs = new ArrayBasedList(4);
ourTAs.addLast("Carrie");
ourTAs.addLast("Evan");
Dillo iMissDillos = new Dillo(6, true);
ourTAs.addLast("Nastassia");
ourTAs.addLast("Put");
ourTAs.addLast("Joe");
```

Draw the memory diagram that will result from trying to run this code. What will happen? What do you want to have happen? What should we do?

