Arrays
Learning Goals
- Understand what an array is and why they are useful
- Create arrays and access data from them
Technical Vocabulary
- Array
- Element
- Index
- Initialize
Warm Up
In a Playground, declare a variable for every scholar in the room. The variable names should be something like scholar1
, scholar2
, etc. and the values should be strings of their first names.
Collections
Collections are data types that hold multiple things - think about your grandma’s cat statue collection - there’s probably more than one cat, right?
In Swift, there are two types of collections: arrays and dictionaries. In this lesson and during our time at camp, we will mainly focus on arrays.
Arrays
An array is an ordered collection that stores multiple values. They are useful whenever you need to keep track of an ordered list of things. This is similar to how we use lists to keep track of tasks in the real world. Think back to the Warm Up - if there are 15 or 20 scholars in the room, writing out a variable for each one can be time-consuming. An array will allow us to store all scholar names in one variable!
An array can store any kind of element - from numbers to strings to … other arrays. Usually, a single array holds a lot of items that are all of the same type.
Turn & Talk
Think about Instagram or TikTok - where might those applications use arrays?
Arrays IRL
It’s very likely that every application you’ve used - on a phone or laptop - utilized arrays in the code that built it.
We can’t see all the code that built every application, but there are some places where it’s very clear that an array would be the best tool to use. Here are some examples from popular sites:
- Instagram uses arrays to hold all the posts for a given user. Alfie the Alpaca has over 700 posts, so the array is over 700 elements long!
- Twitch has an array of recommended channels. They advertise these channels in the top left corner of their landing page.
- TikTok has an array of trending creators stored in their program. TikTok shows the information of the first four creators on the “Discover” page. When a user clicks the arrow to see more, TikTok shows the information of the second four trending creators, etc.
Initializing Arrays
Now that we know a bit about arrays, we want to talk about how to create them. We can initialize them in 1 of 2 ways:
- Empty
- With data
Creating an Empty Array
The code below shows the syntax to initialize, or create, an empty array. In the data types lesson, we talked about creating a variable before you know the value, and if you want to do that, you need to tell Swift what data type that variable will hold. We are using the same concept here:
var trending = [String]()
var followers = [Int]()
The examples above create empty arrays that can, in the future, hold strings and integers, respectively.
If you’ve worked in other languages, you may have noticed that it’s possible to hold different data types in an array. Swift has much more strict rules and doesn’t allow this. The thinking behind this is - if we have a list of things that go together, they should be the same data type. Maybe this is a list of names (strings) or grades that one student got (integers).
Creating an Array with Data
Instead of declaring an empty array, we can also declare an array that starts with data, like this:
// An array of strings:
var trending = ["@thecardguy", "@spencerx", "@avani", "@lorengray"]
// An array of numbers:
var followers = [7, 13.8, 7.1, 36.6]
We can think of each user as an element in an array:
This array is stored in the variable trending
, so anytime we call this variable, we will get back this whole list!
trending
//-> ["@thecardguy", "@spencerx", "@avani", "@lorengray"]
Side note: The lines starting with //->
indicate the return value of the previous code; it’s just a way to notate what happens after writing some code.
Turn and Talk
With your partner, explain the following:
- What is an array?
- Why are they useful?
- How do you initialize an array?
Accessing Information
Each element in an array is automatically assigned a number called an index. This index can be used to access a specific element inside the array. Indices begin at 0 and count up. If we look back at our trending
array, the following would be true:
var trending = ["@thecardguy", "@spencerx", "@avani", "@lorengray"]
- “@thecardguy” has an index of 0
- “@spencerx” has an index of 1
- “@avani” has an index of 2
- “@lorengray” has an index of 3
By using the square brackets, we can use the index to access a specific value in an array.
Thinking back to the visual representation of our array, here’s the index of each element:
var trending = ["@thecardguy", "@spencerx", "@avani", "@lorengray"]
trending[0]
//=> "@thecardguy"
trending[2]
//=> "@avani"
We can also check how many elements are in an array with the .count
property to get the number of elements in a specific array:
var trending = ["@thecardguy", "@spencerx", "@avani", "@lorengray"]
trending.count
//=> 4
Try It: Creating Arrays
In a new Playground, create a variable that stores an array of at least 4 strings - you choose what the content is about. The variable name should describe the type of data those 4 strings hold.
Write a series of print
statements: print out the first element, the last element, and then the second element.
Updating Information
We can also update elements with the square bracket syntax we looked at earlier. We access the index value that we would like to change and then reassign a new value for that index with a =
.
var trending = ["@thecardguy", "@spencerx", "@avani", "@lorengray"]
trending[1] = "@cosette"
print(trending)
//=> ["@thecardguy", "@cosette", "@avani", "@lorengray"]
Adding Information
A common way to add something to an already existing array is to use the .append()
method - which will add an element to the end of the array.
var trending = ["@thecardguy", "@cosette", "@avani", "@lorengray"]
trending.append("@jamescharles")
print(trending)
//-> ["@thecardguy", "@cosette", "@avani", "@lorengray", "@jamescharles"]
In the code snippet above, .append()
is called on the trending
array. We give .append()
an argument of the new element we want to be added to the array. In this case, it was the string of @jamescharles
.
Removing Information
A common way of removing elements is to use the remove(at:) method - which will remove an element at whatever index you type in after the at:
.
var trending = ["@thecardguy", "@cosette", "@avani", "@lorengray", "@jamescharles"]
trending.remove(at: 0)
//-> ["@cosette", "@avani", "@lorengray", "@jamescharles"]
.remove()
was called on the trending
array, and we passed 0
as an argument, indicating that the element in the 0 index position should be removed.
Try It: Modifying Arrays
For this, you will use the array you wrote in the previous Try It.
Practice accessing specific elements. Make sure to print
to verify you are accessing what you think you are.
Now, add two new elements into your array. Use a print
statement to make sure they have been added.
Lastly, remove at least two elements from your array. Again, make sure they have been successfully removed by printing to the console.
Random Elements
Sometimes, we want to pull an element out of an array at random - have any of your teachers ever used popsicle sticks to decide who to call on? That’s a real-life example. We can do the same thing with programming!
Swift gives us a tool called Int.random()
which will help us do that!
Let’s work on generating a random number between 1 and 20.
First, generate a random Integer:
var randomInt = Int.random(in: 1..<20)
//=> 7 (this number will vary)
var anotherRandomInt = Int.random(in: 1..<20)
//=> 19 (this number will vary)
In the code snippet above, Int.random()
was given two pieces of information: 1..<20
. This defines the range that we want a random number to be selected from. When 1..<20
is used, a number starting at 1 and less than 20 will be selected and stored in the variable.
Going back to why would this be useful
? A teacher might have a program with an array of names, like this:
var students = ["Leta", "Ellen Mary", "Pam", "Megan", "Amy", "Sarah", "Robyn", "Courtney", "Rachel", "Allison", "Ruby", "Maile", "Julie", "Meg", "Christie", "Emmie", "Aurora", "Tori", "Juliana", "Kerry"]
Now, the teacher can write a pickStudent
function:
func pickStudent() -> String {
var random = Int.random(in: 1..<20)
var student = students[random] // note about how this work below!
return student
}
var randomStudent = pickStudent()
print(randomStudent)
//=> one random element from the array will print out
Instead of calling students[0]
or students[7]
, we called students[random]
. Since we know that random
is a variable that stores an Integer, the Integer it stores is substituted in for the variable name random
, and then the array looks for the element in that index.
Turn & Talk
Considering this section on selecting random elements from an array, answer the following questions with your partner:
- What does
Int.random()
do? - Why do we have to pass an argument, or put information inside the parenthesis for
Int.random()
? - How would you generate a number between 1 and 100? 300 and 350?
- Is it possible for the
pickStudent
function to ever return the same number? Why or why not?
Putting It All Together
Arrays are a type of collection that developers use on a daily basis. It’s important to have an understanding of their job and the ability to create and identify them. However, keep in mind that even professional developers can’t remember it all and have to use their resources!
Practice: Arrays
Declare a variable called hobbies
that stores an array of your top five favorite accounts to follow on social media, in strings.
- Change the value of at least one of the elements in the array
- Add a new account to the array
- Remove the last account from the array
- Print the value of the third element of the array
- Change the value of another element in the array
- Add another account to the array
- Print the value of the first element of the array
- Print one account to the console, at random.
Spicy Challenge: Write a function that takes in one argument, a string. If the string is "happy", output a randomly generated sentence about being happy. If the string is "ok", output a randomly generated sentence about being ok. If the string is "sad", output a randomly generated sentence to cheer someone up. You will need to use a function, conditional, multiple arrays, and have to generate a random number.
Extension: There are several ways to add, remove, and modify values in an array. There is also a variety of other ways to iterate over arrays. Using Google and the Swift documentation, find a different way to do each of the following:
- add an element
- remove an element
- modify an element