Indexes and the subscript
Every element of an array has a position called its index. Swift arrays use zero-based indexing: the first element is at index 0, the second at index 1, and so on. To read an element, write the array's name followed by the index in square brackets. This is the array's subscript.
let animals = ["cat", "dog", "parrot"]
print(animals[0]) // cat
print(animals[1]) // dog
print(animals[2]) // parrot
An element read this way is an ordinary value of the array's element type. animals[1] is a String, so you can store it in a constant or pass it to a function:
let pet = animals[1]
print(pet) // dog
Using elements in expressions
For an array of Int, each element is an Int, so arithmetic works on elements directly. The whole expression inside print is evaluated first, and print receives the result.
let ages = [14, 32, 50, 21]
print(ages[0] + ages[1]) // 46
The last element
An array with four elements has indexes 0, 1, 2 and 3. The last index is always one less than the number of elements, which you get from count. Both of these refer to the last element of ages:
print(ages[3])
print(ages[ages.count - 1]) // 21
Swift also offers ages.last, but it returns an optional, because an empty array has no last element. Indexing with count - 1 gives a plain Int, which is what these exercises use. Swift does not accept a negative index such as ages[-1] to count from the end.
The exercise, step by step
The starter code declares two constant arrays, and each step adds one print.
- Print the second fruit. The second item has index 1, not 2, so the statement is
print(fruits[1]). It printskiwi. - Print the sum of the first three ages. These are the elements at indexes 0, 1 and 2. Add them inside the call:
print(ages[0] + ages[1] + ages[2]). That prints96, since 14 + 32 + 50 = 96. The element at index 3 is not part of the sum. - Print the difference between the last and the first age. The array holds four numbers, so the last one is at index 3. The statement is
print(ages[3] - ages[0]), which prints7(21 - 14). Keep the order: last minus first. Reversing it gives-7.
let fruits = ["mango", "kiwi", "grapefruit"]
let ages = [14, 32, 50, 21]
print(fruits[1])
print(ages[0] + ages[1] + ages[2])
print(ages[3] - ages[0])
The output is three lines: kiwi, 96 and 7.
Common mistakes
- Counting from 1.
fruits[2]is the third fruit,grapefruit, not the second. - Reading past the end. With four ages,
ages[4]is invalid. The program crashes at run time. The next lessons look at this error more closely. - Using a non-integer index. The index must be an
Int. AStringor aDoubleis a compile error. - Joining numbers as text.
print(ages[0], ages[1])prints the two numbers separated by a space. It does not add them. Use+between the elements.
Key takeaways
- Indexes start at 0, and the last index is
count - 1. - Read an element with
array[index]; the result has the array's element type. - Elements can be used in arithmetic and in any other expression.
- An index outside the array crashes the program.
