Understanding arrays
Let's say you want to store the following:
- A list of items to buy at a convenience store
- Chores that you have to do every month
Arrays would be suitable for this. An array stores values in an ordered list. Here's what it looks like:
Values must be of the same type. You can access any value in an array by using the array index, which starts with 0
.
If you create an array using the let
keyword, its contents cannot be changed after it has been created. If you want to change an array's contents following creation, use the var
keyword.
Now let's see how to work with arrays. You'll create an array by assigning a value to it in the next section.
Creating an array
In previous chapters, you created a constant or variable by declaring it and assigning an initial value to it. You can create an array the same way.
Imagine that your spouse has asked you to get some...