Everybody Mark Price you're at slopes dot com.
Today we are going to build nothing at all.
We're in a code in the playground though.
And we got excited like we're going to build our first iOS app.
I'm sorry I tricked you.
I mean open a new playground here.
Move it on over to the screen.
OK playground.
We're going to call this one.
Re me.
OK.
We're going to talk about race today.
An NRA is incredibly important data structure and foundational data structure that you need to learn.
So we're going to call this raise save it right there.
So let's talk about why we need to raise.
So let's say that we have an app and it manages our employees and our employees all of us our let's
say it's a salary manager so let's we've got our employee one salary.
OK.
And let's say equals forty five thousand dollars a year.
You know our employee two salary equals fifty four dollars.
All right our employee three salary equals $100000 a year for loyalty or salary equals $20000.
This dude's an intern.
He ain't getting paid nothing.
All right.
So we up for employees here.
OK.
So cool.
You know we've got a list of data here but what if I need to work with things or what if I need to fire
somebody.
OK how do I get rid of this.
Well this isn't really helpful is it.
These are just four variables we had are right a lot of code over and over and over again and we have
four employees here.
Maybe it makes sense to structure this in a slightly better way and we can do that.
We can do something we can create something called an array.
And so to declare a you can of course use a constant or very long.
In our case we use a variable.
So we can change it around.
We do just call employees or employee salaries.
All we care about is the salaries right now.
So what we can do is say 45000 Oh comma four thousand one hundred one hundred thousand over 20.
Oh.
Oh OK.
So what we just done here is we've created an array of type and that has four elements in it.
Those are all proper terms.
An array has X amount of elements in it.
OK.
And again this is an array of type.
And so you create one.
You can create an array initialized with data right from the get go by putting the left square bracket
and the right square bracket or brace the left brace and the right brace OK.
And for for terms.
Those are braces and these are brackets.
Technically people do say them interchangeably and sometimes I make mistakes but just know these are
brackets and these are braces now OK.
And then you separate the values in the array with a comma.
What was the what would happen if I tried putting a string in one of these elements here.
Interesting is yelling at me.
No it's not OK.
But what if I said this array has to be of type int.
Now it's yelling at me.
Oh interesting it can't be used with an array literal.
So when I did this it worked when I put it in front of it it did not.
That's because you can store various types in an array.
But if you want to explicitly declare a type of an array it must all have the exact same type and are
in our case we want it to be we want it to be integers.
OK.
Eric Sujan these are integers these are doubles because of the point zero at the end.
OK.
So pretty cool right.
So that's how we create an array and we can do different things with an array.
Let's say we want to add a new employee or rather than creating a whole new variable and play five you
know we could just go do something like this.
Employee employee salaries there is not a pen.
Let's add a new employee salary and let's make this one thirty nine thousand nine thousand dollars and
34 cents a year.
So what we've just done is we've added a fifth element to the array which I think is pretty cool.
So at this point we can actually print some things or we can print the count so employee salaries don't
count at this point in time is for.
And by the way if you see this end here it means new line it's creating a new line for us so the next
thing prints on the next line that's why it's there you don't need to worry about it.
OK.
And then last print after you've added employee salaries don't count.
OK.
It now has five.
There we go.
So it now is five.
OK.
And the doc count is a property it's a variable but it's called a property of the end of an array and
it will tell you how many elements are in the array and this dot is actually calling a function as you
can see with the left parentheses and the right parentheses.
And you can append an element into that array.
Now if this array is a double k am I able.
Oh didn't like that very much did it.
Why is it yelling at me.
Oh my bad.
Area.
So the type is double when it when it's a type of double or array.
You put the braces around it.
OK.
So here if I wanted to append a string like so you shouldn't let me in because it's expecting a double.
All right.
So now what we've done is we've given an explicit type.
This is an array.
See the braces of type double and boom.
There you have it right there.
That's pretty cool.
You can also do different things as well.
Let's see if we can specify it like this.
Interesting it's letting us define it a different way and this is the exact same things we did before.
OK.
So just using different syntax array of type double the more common way is this.
OK.
And then we've appended it.
What if I want to remove an element let's say we just fired employee number two.
So this one right here how do we get rid of this employee.
Well we can to say employee salaries dot remove X and it wants to index.
And we're going to say 1 you're like wait a minute we're not getting rid of the first employee We're
going to get rid of the second employee.
What's going on.
Well here's a secret.
I mean to tell you and it's not a secret at all.
So it's not a secret and you need to know this because it is so important.
Arrays start at zero.
Every array in pretty much every programming language always starts at zero so the zero index is number
one.
So this is 0 1 2 and 3.
OK.
Very important so to get rid of Employee 2 we say 1 and this may be confusing at first but always if
you get confused or is this basically if you want to start at once started one but then remember you
need a minus one because it starts at zero.
OK so we removed unemploy.
So now I say I print the employee salaries don't count.
It should now be back for.
So it's gone.
So you can add things to an array.
You can remove things one way an array should be used whenever you need a collection of something to
be together.
That's a it's a collection that's what it is a collection of things.
OK.
You know so similarly you know if I was a teacher in a classroom which I am oh you might have a list
or an array of students right.
So students.
OK.
And we're going to do this time as we're going to just create an empty array.
So what you do is you put the square braces or just the braces with the type in it and then you put
the parentheses at the end.
Remember this is an equal sign.
What this is doing is it's creating an empty array of type string.
So if I print the count print students dot count it should be zero.
OK this is really important that you initialize an array.
So if I say if I was to do this for students students list to get and I just said it's an array of type
string like that.
And then if I tried to do something like this student list to dot a pen and we say John Adams.
See what happens.
We have a crash.
This will crash or at variable student lists to pass by reference before being initialized.
What that means is you have not initialized it.
This has just been declared and not initialized.
This one is initialized.
This one is not OK as you can see by the parentheses here and this would actually crash application
C always must initialize an array before you can actually use it.
And the same goes for variables any type of variable.
OK.
So this is no bueno.
OK so we've we've now crashed our program was going to crash.
I think it crashed for different reasons.
But so now we can add student straight students that are penned.
You know they say John students got a pen.
Students pen say students type pen
jingle students and crash I was great.
All right.
Students panned either.
Now this may seem like what are you doing.
A lot of stuff here.
But mom this actually saves us a lot of code in the end by using arrays versus creating a bunch of different
variables.
So anyway
we got I just wanted to show you this cool little pyramid here of what happens when you add things so
we add one element two elements three elements four elements 5 and 6.
Ok Allison we want to get rid of Jose because he's clearly not part of this song.
I mean the student.
So we can just say what does he talking about song remember from from grade school.
Jake.
Jingle Hummer's.
Well you're like you guys are like stupid American.
They don't teach that.
Where you're from I get it.
I get it.
What we want to remove Jose because he doesn't belong in the song.
So which index should we get rid of.
Tell me one two three four five or six.
You said two that's right.
Because 2 0 1 2.
So Jose is no longer in our song.
And now if I print the array and say to students
John.
Jean Hammersmith.
OK so lots of fun things you can do with an array and if you want to see all the things that are available
for you when it does command click append here and is going to take us to the documentation and you
can do different things different type of append functions.
Remove the last element insert a new element add an index remove an element add index remove all k a
whole bunch of things you can do with arrays and we've just barely scratched the surface and arrays
are also commonly used hand-in-hand with loops which we haven't talked about yet.
But you'll see that you'll see that to come.
So it's a collection of data allows you to work with different things.
So I think pleura when you think of an array so you know lots of students make an array a list of teachers
make an array a list of anything make an array.
And now you're starting to think of better ways to organize your code rather than doing stuff like this.
OK.
Start using arrays this collection types and that's it.
There's so much more to go into arrays but this is the basic foundational building blocks that I'm hoping
for.
Mark right.
Slopes dot com.
Stay tuned for our exciting next episode.