# Arrays and Lists¶

Arrays are collections of things that belong together. Arrays are found all over math and science. We have collections of antennas, called “antenna arrays,” that are used for listening to things far away.

Large planar array antenna of a VHF Russian mobile air defense radar, the Nebo-M. It consists of 175 folded dipole antennas driven in phase. It radiates a narrow beam of radio waves perpendicular to the antenna. By Vitaly V. Kuzmin, CC BY-SA 4.0

We have arrays of phone switches.

Array of telephone switches and their operators during WWII. Credit: U.S. National Archives

We have arrays of computer memory.

Memory ferrite cores. Non-volatile computer memory with ferrite cores, invented in the late 1940s and used until the 1960s. By Frédéric BISSON, CC BY 2.0

Kids who have worked with multiplication are introduced to arrays that look like this.

Math array used for multiplication. Credit: SVG Silh, CC0 1.0

The items in your array belong together. Think of your kitchen as a collection of arrays. The refrigerator is an array of things that need to stay cool. The freezer is an array of things that need to stay frozen. The pantry is an array of food that can be room temperature. You have an array of plates, an array of cups and glasses, an array of eating utensils, an array of towels, an array of spices, etc.

Arrays also have a size. In my array of cereal, I have 4 boxes of cereal. In my array of silverware, I have arrays of forks (8), salad forks (8), tea spoons (8), soup spoons (7), butter knives (8), and steak knives (8).

In simple arrays like above, each item is in a numbered slot called a “key” or an “index.” In most programming languages, arrays start at zero (0) and count upward until you get to one less than the size of the array. So, for an array of 8 spoons, you would count them as “spoon 0, spoon 1, spoon 2, spoon 3, spoon 4, spoon 5, spoon 6, and spoon 7.” In a Scratch list, you start counting at 1. This can get confusing, but it’s important to understand if that’s how items are arranged in the programming language that you are using, because you get the item in a simple array by its key number.

var movies = [
"Black Panther",
"Coco",
"A Wrinkle in Time"
];

// the first movie in this list, "Black Panther," is:
var firstMovie = movies[0];


We can also have an array of arrays. This is called a “multi-dimensional array.” Imagine a box of donuts. There are probably 12 donuts in that box of donuts. So, you might have:

var donuts = [
"d1", "d2",  "d3",  "d4",
"d5", "d6",  "d7",  "d8",
"d9", "d10", "d11", "d12"
];


The third donut in that box is donuts[2] since we start counting in arrays at zero. But we know that we actually have 3 rows of 4 donuts in this box of donuts:

var donuts = [
["d1", "d2",  "d3",  "d4" ],
["d5", "d6",  "d7",  "d8" ],
["d9", "d10", "d11", "d12"]
];


Now, getting that third donut would require us to identify which row it’s in, too: donuts[0][2]

Question: how do I get the last donut?

// donuts[x][y]; what is "x"? what is "y"?


Bonus Question: if I had 10 boxes of 12 donuts, how would I get the last donut?

// donuts[x][y][z]; what is "x"? "y"? "z"?


Bonus Question, just for Jenna:

// What did Hagrid say to:

[
'Dumbledore',
'McGonagall',
'Snape',
'Moody',
'Tonks',
'Black',
...
] // ?

console.log('You're a wizard array');


Scratch

See info about the Scratch version of an array, the list block.