Arrays
Often, we want to work with a list of data, such as a list of items the player possesses. An array is precisely the data structure we want to use for such occasions: it is a list that can contain elements from other data types; it is a container type.
Containers
We call an array a container because we can store and retrieve pieces of data of other data types within them, such as integers, strings, Booleans, and such. An array contains other data.
Containers structure other data so that it is easier to work with.
Let’s have a look at what an array is in code and how we can create one and access its elements.
Creating an array
Creating an array looks like this:
var inventory = ["Key", "Potion", "Red Flower", "Boots"]
Here, we created an array – a list of four strings – and put it in the inventory
variable. Notice that all the elements of the array are contained within square brackets, []
, and that...