What is an array and why is it important?
To quote from Apple's Swift documentation, "An array stores values of the same type in an ordered list. The same value can appear in an array multiple times at different positions." Translation: An array will store all strings, all integers, or all floats, but will not store a mix of strings, integers, or floats. What's an ordered list? That means that Swift assigns an order to the string, integer, or float values you have in your array. The first string in an array is 0, the second string is 1. What? That doesn't make sense? No problem. I'll post an example below in a minute. What else should you know about arrays? Arrays can have the same value more than once. If you make an array of a team's roster by surname and you have more than one Martinez, Smith, or Jackson, the array can handle it. How does the array tell them apart? Through the unique index numbers assigned to each Martinez, Smith, or Jackson. OK. That's a good start. Let's jump into an example.

Say you wanted to create an array collection type to keep track of all the new Baby Bombers that the Yankees have called up this year. We'll start by creating an array, informing the computer that we will fill the array with strings, and then set that array to a variable called babyBombers.

See those [ ] brackets? Those brackets indicate that our babyBomber variable is going to be an array. Now let's fill or initialize that array with the name of a Baby Bomber.

There! We've added Gary Sanchez to the array. But what about Aaron Judge, Tyler Austin, Ben Heller, Ronald Torreyes, Luis Cessa, and Chad Green? Let's add them to the babyBomber array.

How'd we do that? We took three steps. First, we dropped a "." after the name of our array. This is like Sanchez putting his hand down to call a pitch. Want to see what other kinds of pitches you can call with that "."? After the last line above, type babyBombers. and take a gander at just some of your options:

What you see there is just a fraction of the functions you can call after that "." If you scroll down within that box you'll see many more. For now, we'll focus on the append function.
After the "." we typed append. And lastly, we told append exactly what to add to our babyBomber array by passing an argument. In our cases, the arguments are the names of the players. We pass arguments with ( ) parentheses immediately after the function keyword append.

You can see here, highlighted in blue, that Xcode shows us the append function and how if we do choose to append, that we'll need to include a new string element such as "Sanchez", "Judge", or the name of another Baby Bomber if we want to add it to our babyBomber array.
What if we want to remove Tyler Austin from the array? How would we do that? Let's give it a shot.

And Austin's gone! But how'd we do that? First, on line 14, we called the removeAtIndex function which, like the append function, takes an argument (). But wait, why can't we just pass "Austin" as the argument? Good question. Here's what that would look like:

In other words, we can't pass "Austin" as the argument to remove Austin because the removeAtIndex function only takes an integer or Int. But how do we know what Int Austin is? Another good question.
To answer that we need to think of the entire array as an index. In the back of some books you may find an index. That index is a complete list of all the topics addressed in the book.

While a book's index is arranged alphabetically, an array's index is arranged numerically starting with zero (0). In this case, "Sanchez" is 0, "Judge" is 1, and "Austin" is 2. So when we pass 2 as our removeAtIndex argument that's how we tell the array to remove Austin.
As you may have noticed when you typed "babyBomber. " into line 14 of your playground and saw all the different function options you had, there are a lot. I'll go over a few particularly popular ones here, but I highly recommend playing with the rest to find out what they do. I've found one of the best ways to learn is to just try things out to see what they do.
What if, say, you had the entire Yankee farm system in your array and you wanted to know how many players total you had. How would we do that? Well, we'd call the count function.

In this case, there are seven players in our babyBomber array. What if you want to add a little extra information to one of your Baby Bombers in the array? Say, you want to add "Jr." to Austin (he's not a junior, work with me).

How did we do that? First, we chose the array (babyBombers) then we chose where in the array we wanted to add something, in this case [2], and then, because our array is a variable full of strings, we told it exactly what we wanted to add. In this case, we want to add a comma immediately after Austin, then a space followed by Jr. And what about that +=? What's going on there? That's a little short hand. The long hand is, "babyBombers[2] = babyBombers[2] + ", Jr." In other words, we are changing the value of babyBombers[2] by adding ", Jr." to "Austin".
What if we want to replace one of the names with another name and keep it in the same spot in the index? For example, let's replace Tyler Austin with Derek Jeter's unborn son, Jeter, Jr.

Like last time, we chose the array we wanted to edit, babyBombers, we chose which value in the array we want to edit [2], and then we assigned a new string value to that array, "Jeter, Jr."
What if we want to add an array of Yankee September call-ups to our babyBomber array? Let's take a look at that.

How'd we do that? First, we created a new array, septemberCallups, and filled it with strings such as, "Young, Jr.", "Mitchell", and "Severino". Then we set that array to the variable septemberCallups. Next we setup a for loop that goes through the septemberCallups array for each string value, or in our case, each of the three callups, and then appends it to the end of the babyBomber array which we can on the right of line 19.
We've appended new array items to the end of the array, but what if we want to insert a new item to the array in a specific place? Let's try that by inserting "Severino" after "Judge".

Before we complete this, take a look above at what Xcode offers us after we type ".insert". Xcode asks us for two things: to enter our new element which is a string ("Severino") and an integer to identify where in the array it should go. Now let's finish inserting Severino.

And there's Luis Severino on the right hand side of line 14 right after Aaron Judge and before Tyler Austin.
Now let's say you're working with a friend on a project and the goal of that project is to create a master array of everyone who has ever played for the Yankees. Your friend has amassed a big list that she's put into an array and so have you. Now you want to see if the two lists are the same. If they are, there's a chance you've got everyone. If not, you'll have to iron out who is missing whom. How do we check to see if the two arrays are equal? Let's take a look.

For the sake of time, we'll assume small lists of Yankees, one for Bob's list of Bombers and one for Jill's list of Bombers. A quick eye-test concludes that they both have all the same players, but Xcode tells us that the two arrays are not equal. Why?
Remember how arrays are ordered lists, in other words, that their orders matter? Well, Bob and Jill may have the same players, but they are listed in different orders so according to Xcode they are not equal. Bob and Jill will have to try another collection type to solve their problem.
Challenge: Re-write these examples with your team's rookies and Tweet them to me (@randallmardus).
On Deck: The dictionary collection type.
No comments:
Post a Comment