Learn Swift for Beginners - Ep 16 - Arrays

Sharing buttons:

hello welcome to learn Swift for

beginners lesson 16 today you're going

to learn about how to manage a

collection of data in what's called an

array if you're working with many pieces

of data it would be hard to manage them

with simply constants and variables so

let's take a look at how a race can make

our life easier alright let's get

started now arrays are one of three

collection types that are available in

the Swift programming language for us to

use and manage our data in this language

guide you can see a diagram of this

array on the far left side we're going

to come back to this diagram in a second

but first a definition so you can think

of an array as a collection of data that

is ordered by indexes now if that

doesn't tell you too much let's jump

right into a swift playground and I'll

show you exactly how beneficial they are

and also had to clear them and how to

use them so first I'm going to delete

this default variable here and we're

going to create a couple of variables

ourselves so let's say VAR a equals dog

VAR b equals can't or C equals bird and

now let's say I wanted to concatenate or

add the word my in front of each of

those values there so that I would have

my dog my cat and my bird so I would

have to do something like this I would

have to go a equals my space plus a and

this would result in a being my dog

because we're adding the word mine with

a space to a and a is dog and then we're

reassigning that result into a again

thereby overriding what was there before

so now a is actually my dog I would have

to repeat this for cat and I'd have to

repeat this with Birk I can't even use

what we learn in the previous lessons in

regards to loops to make my life easier

I'd have to write this

three times for each of the variables so

here's the perfect chance to use an

array to organize this collection of

data so in order to create an array with

the data already in it

we're going to open up two angle

brackets or square brackets if you'd

prefer and inside these two square

brackets we put each piece of data

separated by a comma so we have dog we

have cat and we have bird so just like

that we have an array with three pieces

of data and if you remember what I said

in the definition that arrays are a

collection of data organized by indexes

what do I mean by that well you can see

that there are three pieces of data here

so there's three distinct spots you can

think of it the leftmost spot here the

beginning is index 0 or spot 0 if you'd

prefer to think of it like that the next

one is index 1 and the last one is index

2 so arrays start at 0 and because there

are 3 items here the indexes go from 0 1

to 2 and now if I quickly bring up that

language guide again you can see in this

diagram that in this array there are 5

items and so the index is 4 6 eggs is 0

index for milk is 1 and so on until it

which is 4 even though there are 5 items

because it's zero-based ok so let's go

back down here so this is great that we

haven't know right here but we need some

way to reference that array so actually

what we do is we can create a variable

let's call it D and we assigned this

array or this collection of data into

the variable D so now if I wanted to

access dog for example I would write D

and then I would write square brackets

like that and in between the square

brackets I would put an integer

representing the index of the item that

I want so let's say I want dog I would

put 0 so you can see here I would get

dog right and so we can print that out

and that would print dog down here

if I change the index to one then I

would get cat instead now let's do an

example where we have something like

this just to duplicate that I would say

let's say a equals my plus D 0 that I

can do B equals my that's my cat and

finally I would get my bird but then I

mentioned that there was a better way to

do it if we leverage what we learned in

the previous lesson on loops well we can

let's take a look at using for loops and

simplifying our work here so remember

for loops will loop a piece of code for

a specified number of times and you can

see here that I'm working with index 0

and x1 index 2 so this becomes really

easy I can say for remember the next the

next piece of the for loop is a counter

so no it's my variable to hold the

current index and then you write in and

then you write your range so I can write

0 0 sorry I mean 0 2 and this is going

to loop from 0 to 2 so I think you can

kind of see where I'm getting at what

I'm going to do is print my plus D and

inside here where I put the index

usually I'm going to put counter and you

can see here took the playground a

little second but that's exactly what I

expected to do here so in the first

iteration of this for loop counter is 0

right that's the starting range so 0

gets past the knee here and I would get

this printed out this is dog D at index

0 right is dog in the next iteration of

the loop counter is 1 and so I'm

actually accessing index 1 of my array

so that's why I get cat and then finally

it loops again and counters - and I

would access this bird index here I want

to show you another way to use your for

loop with an array and that's simply to

say for item in D so what this is going

to do is it's going to loop through all

of the items in the array D and in each

iteration of the loop it's going to take

that item or that piece of data and it's

going to assign it to item so I can

simply go like this so you can see that

it gets printed out again right in the

first iteration item is dog in the

second iteration it's cat and the third

it's bird so this is pretty simple way

to write it and you can see that it

saves a lot of work from doing it kind

of one by one like this and one by one

like this arrays in conjunction with

loops really powerful stuff now with a

race there's other cool things you can

do let me just make some space here

maybe I should delete this stuff you can

actually declare an empty array so it's

an array that would contain no data at

first and the way you do that is just

like storing things into a variable or a

constant arrays can only store data of a

certain data type that you specify so

since it's an empty array how you would

do it is you open up two square brackets

you put the data type inside the two

square brackets and that data type

represents the type of the data that the

array is going to store so I'm just

going to put string here and then you

end off with two round brackets like

that and just like that now e refers to

an array that is empty right now doesn't

contain any data we

the intention of storing string type

data in this array now if you're going

to create an empty array like this you

better be able to add data to that array

right so what makes arrays really useful

is that you can add and remove data from

that collection so I can add or remove

from this collection right here I can

add or remove from this collection here

let me just show you how to do that

there are a couple of different ways I

can do something like this D plus equals

to angle brackets like that again and

let's say I wanted to add malleus and

now my D array would contain four items

as you can see here dog cat bird and

mouse in fact I can even add two pieces

of data at once comma and then here I

can put a lull for example so it would

add mouse and owl to that array so now

my array has five items from zero to

four right starts at zero one two three

four make sure that you don't forget

that plus sign right here because if you

do that then essentially you're creating

a brand new array with these two items

and you're assigning it to D and you've

just lost this data here so plus equal

is for adding items now seeing this plus

equals you might be tempted to use minus

equals to remove items like this but

that actually doesn't work unfortunately

you can't remove items from the array

like that what you have to do is the

array actually has functions that you

can call on it to remove items so you

would say D and then you would press dot

on your keyboard or the period key and

out pops a list of functions that you

can call on this array and using the

append function is going to do exactly

like this plus equal is it's going to

add items into that array but let's look

at the remove function so you have

remove all which is going to remove all

the items in our array

but you can use this one here remove at

and you can specify the index of the

item you want to remove so if I put zero

like that that's going to remove dog

from my array so now it's only going to

contain cat bird mouse and owl now what

if I don't want to completely remove dog

but I just wanted to change that element

right there at index zero so let me get

rid of this removed line you saw that

you can access items in the array by

doing that right putting in the index

here well you can actually change the

item you can change what is assigned at

that index by typing d square brackets

put in the index you want to change and

using the equal sign to assign something

new into that spot so here let's say

turtle and that is going to now change

your array if I print d 0 i'm going to

get turtle instead of dog because i just

changed it up here the last thing i want

to point out is that arrays also you can

check how many items are in there if you

look at the count that's going to return

the number of items in your rate that is

sometimes useful when you want to use a

for loop with a range and you don't know

how many items are in the array you can

use this array dot count and get this

number here but just keep in mind that

although D has five items right here the

index of the last item is actually only

four because the first item is zero it

goes from zero one two three four right

even though there are five items so just

keep that in mind if you're going to use

this array dot count in conjunction with

a for loop or something like that okay

so that's where we're going to end with

arrays as you can see when you type

array dot there are a lot of different

functions with arrays that you can do

what I've covered here in this lesson is

enough for you to use a rate and

leverage some of the main benefits of a

race as we go on and we're building apps

together you're going to be learning new

ways to use a race but for now these are

the main things you need to know about a

race in order to start using them if you

like this video please give this video a

thumbs up please subscribe for more and

I'll see you in the next lesson bye for