Like the String class, arrays come with a lot of built-in functionality that you’ll use on a daily basis. In this video, learn about those concepts by creating an array and manipulating it in different ways.
- [Male Instructor] Now that we know how to create and access arrays, we also need to learn how to manipulate them. Like the string class, the array class comes with a lot of handy methods out of the box. So we can use append, insert, remove, and sort among others. Let's start by creating a new variable, called characterClasses. And we're going to initialize this with a few values. I'm going to say Ranger, Paladin, and Druid.
I'm going to print these out just so we can see clearer in the debug log. Now let's use the subscripts and text here to change the second value, Paladin, to another string. So, we'll say characterClasses at index one, now should equal Knight. Keep in mind, that arrays are zero indexed. So, Ranger is at index zero, and Paladin is at index one.
We can also add other items or elements to the array, by using the append function. So here I'm just going to append a new class called Gunslinger. Now append is going to put the new item at the very end of the array, since it's an ordered list. As you might have guessed, we can also use the compound operator plus equals to do this as well. So characterClasses += and open and close brackets and let's say Healer and Berserker.
Now the shorthand syntax here with the compound assignment is really handy, because we can add on more than one item. With the append method, we can only add a single item. Like we saw with strings, we can also insert and remove items from an array. So let's say, characterClasses insert, and we're going to choose the first option here, newElement at index. So our new element is going to be Beast Master, and we're going to put this at position two.
So now in our debug log, we have Ranger, Knight, and Beast Master at position two. For removing, it's pretty much the same thing. CharacterClasses remove at, and let's remove our item at one. So now our Knight has been removed from the array. There are a couple other methods that are really useful here. One of them is a reverse method.
So since arrays are ordered lists, we can simply reverse the order with this call. We can also quickly check if a certain item is contained in our array. So, characterClasses contains, and let's see if it contains gunslinger. Now this is false because we have a lowercase g here. So, if I change it to uppercase, we'll have a return true value. The final thing we want to explore, is how to deal with an array that stores arrays as it's value.
This is referred to as a two-dimensional array. So here I'm going to create a new variable, called skillTree. And I'm going to assign it a type annotation of an array and in here each item is going to be an array of strings. Now, I know this looks confusing. We're going to flesh this out right now. I've indented the open and close brackets for this because it's easier to visualize a two-dimensional array this way.
So here in the first item, which is an array itself, we're going to have Attack+, Barrage, and Heavy Hitter. Now, let's add a comma after this first item, and we'll add in a second array item. Here we're going to say Guard+, Evasion, and Run Like Hell.
Now the question is, how do we access this? Well, we basically chain subscripts together to get down to whatever level of the two-dimensional array we want. So, let's create a new variable called attackTreeSkill. And we're going to set this to skillTree. The first item, at index zero, and then the index of that item. So we're going to say two. Now you'll see that attackTreeSkill is equal to Heavy Hitter.
So, let's break this down. We went into skillTree, we took the item at index zero, which is an array itself, and then we took the item at index two, which is Heavy Hitter. Just a note, in 2D arrays, each of these does not have to be the same length. So for instance, we could have four items in our first item, and three in our second. So let's add another one.
However, with two-dimensional arrays like this, you do need to have all the individual items inside the arrays be of the same type. So in this case, all our items are type string.
- Starting new playgrounds and projects
- Variables and constants
- Writing single and multiline comments
- Core string methods
- Working with numbers
- Working with collections
- Creating arrays
- Working with sets
- Application control flow
- Writing functions
- Basic Swift classes and structs