Comp 110 Arrays: Overview

Arrays

Overview

Arrays let us hold multiple values of the same type in one place. The items in the array are the elements, which we access by index. The index is the position in the array where the value is at. An important note to keep in mind is that indexing starts at zero! This means the first element will be at index 0 and the last index of the array will be the length of the array - 1.

Declaring

When we make an array, we can store it in a variable just like we've been doing with individual values. To declare a variable that'll hold an array, it's type is going to be the type of the values we want to hold in the array followed by square brackets []:

let <arrayName>: <elementType>[];

Here's an example:

let groceryStore: string[];

Constructing

We also use square brackets to construct the new array. If we want to make an empty array, we simply use the empty square brackets with nothing inside. Back with variables that held one value we could initialize it within the same line or separately, and the same is true for arrays. Here's the groceryStore example again:

// declare
let groceryStore: string[];
 
// construct and assign
groceryStore = [];

In this example, we first declared the variable and specified that it would be a string array. Then we constructed an empty array [] and assigned it to groceryStore.

Here's how we'd do all this together:

// declare, construct, and initialize all in one line
let groceryStore: string[] = [];

This is great, but it's still just an empty array. To assign values to be in the array, we specify the name of the array we want to put the value in and the index we want it to be at:

<arrayName>[<index>] = <value>;

 Back to our example:

// declare, construct, and initialize with the empty array
let groceryStore: string[] = [];
 
// fill in the values
groceryStore[0] = "cereal"; // indexing starts at 0!
groceryStore[1] = "drinks";
groceryStore[2] = "bread";
groceryStore[3] = "snacks";

Now, we've given a string value for each index in our array. That's nice, but it can take a while to put each element into the array separately, one by one. Luckily, we can do all this when we construct the array instead of just making an empty array then going back later to fill it in. To do so, we just put the values in the brackets we used to construct the array:

let <arrayName>: <elementType>[] = [<value>, <anotherValue>,...];

With the example:

let groceryStore: string[] = ["cereal", "drinks", "bread", "snacks"];

This makes an array, fills it with the specified values, and assigns this array to the string[] variable groceryStore.

Lists vs Arrays

What do we gain from using an array over a list?

  • We can access ANY index of the array with a single operation, as opposed to starting at the beginning and recursively calling first()/rest().
    • This is more efficient computationally (1 step vs n-steps) and more concise to read/code
  • In Typescript, arrays can dynamically grow in size: myArray[length] = <value>
    • In other languages, arrays are constructed with a fixed size...this can be inconvenient and tedious

What is the difference between the structures?

  • Arrays have indexed elements of data taking up a contiguous block of memory that can be immediately accessed
    • i.e. 10 values taking up 10 spaces of memory
    • Each index has a designated position in the array, but is not necessarily related to the values surrounding it, unlike a list
  • Lists are ordered, linked nodes of data
    • In order to get to the 5th node in a list, we first have to process nodes 0-4...5 steps as opposed to 1
    • Each node is 'linked' to the nodes surrounding it