1
00:00:01,380 --> 00:00:05,270
Everybody Mark Price you're at slopes dot com.

2
00:00:05,300 --> 00:00:10,790
Today we are going to build nothing at all.

3
00:00:10,890 --> 00:00:13,410
We're in a code in the playground though.

4
00:00:15,580 --> 00:00:18,560
And we got excited like we're going to build our first iOS app.

5
00:00:18,560 --> 00:00:20,990
I'm sorry I tricked you.

6
00:00:21,200 --> 00:00:23,990
I mean open a new playground here.

7
00:00:24,440 --> 00:00:25,920
Move it on over to the screen.

8
00:00:26,030 --> 00:00:26,750
OK playground.

9
00:00:26,750 --> 00:00:29,270
We're going to call this one.

10
00:00:29,560 --> 00:00:31,370
Re me.

11
00:00:31,400 --> 00:00:32,310
OK.

12
00:00:32,620 --> 00:00:32,870
OK.

13
00:00:32,870 --> 00:00:34,990
We're going to talk about race today.

14
00:00:35,250 --> 00:00:42,140
An NRA is incredibly important data structure and foundational data structure that you need to learn.

15
00:00:42,290 --> 00:00:47,790
So we're going to call this raise save it right there.

16
00:00:47,800 --> 00:00:52,640
So let's talk about why we need to raise.

17
00:00:52,840 --> 00:01:03,150
So let's say that we have an app and it manages our employees and our employees all of us our let's

18
00:01:03,150 --> 00:01:10,310
say it's a salary manager so let's we've got our employee one salary.

19
00:01:10,370 --> 00:01:11,130
OK.

20
00:01:11,570 --> 00:01:17,750
And let's say equals forty five thousand dollars a year.

21
00:01:18,060 --> 00:01:28,510
You know our employee two salary equals fifty four dollars.

22
00:01:30,800 --> 00:01:46,920
All right our employee three salary equals $100000 a year for loyalty or salary equals $20000.

23
00:01:47,050 --> 00:01:47,980
This dude's an intern.

24
00:01:47,980 --> 00:01:49,180
He ain't getting paid nothing.

25
00:01:49,180 --> 00:01:49,720
All right.

26
00:01:49,960 --> 00:01:52,410
So we up for employees here.

27
00:01:52,450 --> 00:01:52,770
OK.

28
00:01:52,780 --> 00:01:53,970
So cool.

29
00:01:53,980 --> 00:01:58,510
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

30
00:01:58,510 --> 00:01:59,240
somebody.

31
00:01:59,450 --> 00:02:00,660
OK how do I get rid of this.

32
00:02:00,670 --> 00:02:02,270
Well this isn't really helpful is it.

33
00:02:02,270 --> 00:02:09,010
These are just four variables we had are right a lot of code over and over and over again and we have

34
00:02:09,010 --> 00:02:09,800
four employees here.

35
00:02:09,820 --> 00:02:14,990
Maybe it makes sense to structure this in a slightly better way and we can do that.

36
00:02:15,070 --> 00:02:18,320
We can do something we can create something called an array.

37
00:02:18,810 --> 00:02:23,620
And so to declare a you can of course use a constant or very long.

38
00:02:23,710 --> 00:02:25,510
In our case we use a variable.

39
00:02:25,570 --> 00:02:26,930
So we can change it around.

40
00:02:26,990 --> 00:02:30,330
We do just call employees or employee salaries.

41
00:02:30,340 --> 00:02:32,080
All we care about is the salaries right now.

42
00:02:32,080 --> 00:02:46,730
So what we can do is say 45000 Oh comma four thousand one hundred one hundred thousand over 20.

43
00:02:46,940 --> 00:02:47,490
Oh.

44
00:02:47,860 --> 00:02:48,830
Oh OK.

45
00:02:48,880 --> 00:02:55,680
So what we just done here is we've created an array of type and that has four elements in it.

46
00:02:55,690 --> 00:02:57,380
Those are all proper terms.

47
00:02:57,550 --> 00:03:00,440
An array has X amount of elements in it.

48
00:03:00,820 --> 00:03:01,230
OK.

49
00:03:01,240 --> 00:03:02,900
And again this is an array of type.

50
00:03:02,930 --> 00:03:04,340
And so you create one.

51
00:03:04,660 --> 00:03:10,900
You can create an array initialized with data right from the get go by putting the left square bracket

52
00:03:10,960 --> 00:03:16,270
and the right square bracket or brace the left brace and the right brace OK.

53
00:03:16,690 --> 00:03:19,020
And for for terms.

54
00:03:19,080 --> 00:03:21,670
Those are braces and these are brackets.

55
00:03:21,670 --> 00:03:26,350
Technically people do say them interchangeably and sometimes I make mistakes but just know these are

56
00:03:26,350 --> 00:03:29,460
brackets and these are braces now OK.

57
00:03:29,460 --> 00:03:32,720
And then you separate the values in the array with a comma.

58
00:03:32,740 --> 00:03:37,450
What was the what would happen if I tried putting a string in one of these elements here.

59
00:03:43,560 --> 00:03:46,090
Interesting is yelling at me.

60
00:03:46,170 --> 00:03:47,930
No it's not OK.

61
00:03:48,300 --> 00:03:56,620
But what if I said this array has to be of type int.

62
00:03:56,640 --> 00:03:57,830
Now it's yelling at me.

63
00:03:58,020 --> 00:04:01,440
Oh interesting it can't be used with an array literal.

64
00:04:01,440 --> 00:04:06,870
So when I did this it worked when I put it in front of it it did not.

65
00:04:06,870 --> 00:04:10,080
That's because you can store various types in an array.

66
00:04:10,360 --> 00:04:16,080
But if you want to explicitly declare a type of an array it must all have the exact same type and are

67
00:04:16,140 --> 00:04:20,060
in our case we want it to be we want it to be integers.

68
00:04:20,220 --> 00:04:20,970
OK.

69
00:04:21,570 --> 00:04:25,630
Eric Sujan these are integers these are doubles because of the point zero at the end.

70
00:04:25,780 --> 00:04:27,000
OK.

71
00:04:27,750 --> 00:04:28,860
So pretty cool right.

72
00:04:28,890 --> 00:04:32,310
So that's how we create an array and we can do different things with an array.

73
00:04:32,310 --> 00:04:36,090
Let's say we want to add a new employee or rather than creating a whole new variable and play five you

74
00:04:36,090 --> 00:04:38,790
know we could just go do something like this.

75
00:04:38,790 --> 00:04:45,610
Employee employee salaries there is not a pen.

76
00:04:45,630 --> 00:04:51,960
Let's add a new employee salary and let's make this one thirty nine thousand nine thousand dollars and

77
00:04:51,960 --> 00:04:53,470
34 cents a year.

78
00:04:53,970 --> 00:05:00,360
So what we've just done is we've added a fifth element to the array which I think is pretty cool.

79
00:05:00,360 --> 00:05:07,830
So at this point we can actually print some things or we can print the count so employee salaries don't

80
00:05:07,830 --> 00:05:13,070
count at this point in time is for.

81
00:05:13,070 --> 00:05:17,100
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

82
00:05:17,100 --> 00:05:20,110
thing prints on the next line that's why it's there you don't need to worry about it.

83
00:05:20,330 --> 00:05:21,280
OK.

84
00:05:21,870 --> 00:05:26,260
And then last print after you've added employee salaries don't count.

85
00:05:26,720 --> 00:05:27,750
OK.

86
00:05:28,330 --> 00:05:32,370
It now has five.

87
00:05:32,380 --> 00:05:33,260
There we go.

88
00:05:33,310 --> 00:05:34,520
So it now is five.

89
00:05:34,630 --> 00:05:35,610
OK.

90
00:05:35,620 --> 00:05:41,590
And the doc count is a property it's a variable but it's called a property of the end of an array and

91
00:05:41,590 --> 00:05:46,750
it will tell you how many elements are in the array and this dot is actually calling a function as you

92
00:05:46,750 --> 00:05:49,640
can see with the left parentheses and the right parentheses.

93
00:05:50,170 --> 00:05:52,250
And you can append an element into that array.

94
00:05:52,270 --> 00:05:59,930
Now if this array is a double k am I able.

95
00:06:00,480 --> 00:06:02,680
Oh didn't like that very much did it.

96
00:06:02,970 --> 00:06:04,130
Why is it yelling at me.

97
00:06:04,390 --> 00:06:05,920
Oh my bad.

98
00:06:07,280 --> 00:06:07,910
Area.

99
00:06:08,150 --> 00:06:11,910
So the type is double when it when it's a type of double or array.

100
00:06:11,960 --> 00:06:14,710
You put the braces around it.

101
00:06:14,750 --> 00:06:15,320
OK.

102
00:06:15,560 --> 00:06:23,390
So here if I wanted to append a string like so you shouldn't let me in because it's expecting a double.

103
00:06:23,570 --> 00:06:25,400
All right.

104
00:06:25,400 --> 00:06:29,060
So now what we've done is we've given an explicit type.

105
00:06:29,060 --> 00:06:30,950
This is an array.

106
00:06:31,020 --> 00:06:34,600
See the braces of type double and boom.

107
00:06:34,670 --> 00:06:36,090
There you have it right there.

108
00:06:36,090 --> 00:06:37,550
That's pretty cool.

109
00:06:37,580 --> 00:06:39,050
You can also do different things as well.

110
00:06:39,110 --> 00:06:42,420
Let's see if we can specify it like this.

111
00:06:46,580 --> 00:06:51,620
Interesting it's letting us define it a different way and this is the exact same things we did before.

112
00:06:51,620 --> 00:06:51,960
OK.

113
00:06:51,980 --> 00:06:57,480
So just using different syntax array of type double the more common way is this.

114
00:06:58,160 --> 00:06:59,180
OK.

115
00:06:59,870 --> 00:07:00,890
And then we've appended it.

116
00:07:00,920 --> 00:07:06,030
What if I want to remove an element let's say we just fired employee number two.

117
00:07:06,080 --> 00:07:08,930
So this one right here how do we get rid of this employee.

118
00:07:08,930 --> 00:07:15,590
Well we can to say employee salaries dot remove X and it wants to index.

119
00:07:15,620 --> 00:07:21,980
And we're going to say 1 you're like wait a minute we're not getting rid of the first employee We're

120
00:07:21,980 --> 00:07:23,480
going to get rid of the second employee.

121
00:07:23,480 --> 00:07:24,480
What's going on.

122
00:07:24,830 --> 00:07:26,940
Well here's a secret.

123
00:07:27,220 --> 00:07:29,360
I mean to tell you and it's not a secret at all.

124
00:07:29,570 --> 00:07:32,680
So it's not a secret and you need to know this because it is so important.

125
00:07:32,690 --> 00:07:34,210
Arrays start at zero.

126
00:07:34,250 --> 00:07:40,220
Every array in pretty much every programming language always starts at zero so the zero index is number

127
00:07:40,220 --> 00:07:40,520
one.

128
00:07:40,520 --> 00:07:45,150
So this is 0 1 2 and 3.

129
00:07:45,440 --> 00:07:45,780
OK.

130
00:07:45,800 --> 00:07:51,230
Very important so to get rid of Employee 2 we say 1 and this may be confusing at first but always if

131
00:07:51,230 --> 00:07:55,700
you get confused or is this basically if you want to start at once started one but then remember you

132
00:07:55,700 --> 00:07:57,570
need a minus one because it starts at zero.

133
00:07:57,740 --> 00:07:59,700
OK so we removed unemploy.

134
00:08:00,170 --> 00:08:07,220
So now I say I print the employee salaries don't count.

135
00:08:07,400 --> 00:08:09,260
It should now be back for.

136
00:08:09,440 --> 00:08:10,850
So it's gone.

137
00:08:11,360 --> 00:08:13,670
So you can add things to an array.

138
00:08:13,670 --> 00:08:18,710
You can remove things one way an array should be used whenever you need a collection of something to

139
00:08:18,710 --> 00:08:19,370
be together.

140
00:08:19,380 --> 00:08:22,760
That's a it's a collection that's what it is a collection of things.

141
00:08:22,760 --> 00:08:23,310
OK.

142
00:08:23,570 --> 00:08:32,660
You know so similarly you know if I was a teacher in a classroom which I am oh you might have a list

143
00:08:32,690 --> 00:08:34,080
or an array of students right.

144
00:08:34,090 --> 00:08:35,580
So students.

145
00:08:35,600 --> 00:08:36,160
OK.

146
00:08:36,440 --> 00:08:41,700
And we're going to do this time as we're going to just create an empty array.

147
00:08:41,720 --> 00:08:46,460
So what you do is you put the square braces or just the braces with the type in it and then you put

148
00:08:46,460 --> 00:08:48,650
the parentheses at the end.

149
00:08:48,680 --> 00:08:49,860
Remember this is an equal sign.

150
00:08:49,880 --> 00:08:53,740
What this is doing is it's creating an empty array of type string.

151
00:08:53,750 --> 00:08:59,850
So if I print the count print students dot count it should be zero.

152
00:09:00,190 --> 00:09:04,470
OK this is really important that you initialize an array.

153
00:09:04,520 --> 00:09:11,850
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

154
00:09:12,200 --> 00:09:13,420
string like that.

155
00:09:13,700 --> 00:09:22,620
And then if I tried to do something like this student list to dot a pen and we say John Adams.

156
00:09:22,830 --> 00:09:24,880
See what happens.

157
00:09:25,070 --> 00:09:25,930
We have a crash.

158
00:09:25,930 --> 00:09:30,240
This will crash or at variable student lists to pass by reference before being initialized.

159
00:09:30,410 --> 00:09:32,270
What that means is you have not initialized it.

160
00:09:32,270 --> 00:09:36,100
This has just been declared and not initialized.

161
00:09:36,110 --> 00:09:37,480
This one is initialized.

162
00:09:37,520 --> 00:09:41,900
This one is not OK as you can see by the parentheses here and this would actually crash application

163
00:09:41,900 --> 00:09:45,920
C always must initialize an array before you can actually use it.

164
00:09:45,920 --> 00:09:48,410
And the same goes for variables any type of variable.

165
00:09:48,530 --> 00:09:49,070
OK.

166
00:09:49,220 --> 00:09:51,360
So this is no bueno.

167
00:09:51,980 --> 00:09:57,850
OK so we've we've now crashed our program was going to crash.

168
00:09:58,160 --> 00:09:59,320
I think it crashed for different reasons.

169
00:09:59,330 --> 00:10:03,560
But so now we can add student straight students that are penned.

170
00:10:03,860 --> 00:10:10,070
You know they say John students got a pen.

171
00:10:11,230 --> 00:10:17,770
Students pen say students type pen

172
00:10:20,430 --> 00:10:32,630
jingle students and crash I was great.

173
00:10:32,730 --> 00:10:33,470
All right.

174
00:10:33,960 --> 00:10:41,400
Students panned either.

175
00:10:41,520 --> 00:10:43,260
Now this may seem like what are you doing.

176
00:10:43,260 --> 00:10:43,940
A lot of stuff here.

177
00:10:43,950 --> 00:10:48,900
But mom this actually saves us a lot of code in the end by using arrays versus creating a bunch of different

178
00:10:48,900 --> 00:10:50,630
variables.

179
00:10:50,850 --> 00:10:51,210
So anyway

180
00:10:54,150 --> 00:10:59,150
we got I just wanted to show you this cool little pyramid here of what happens when you add things so

181
00:10:59,150 --> 00:11:02,900
we add one element two elements three elements four elements 5 and 6.

182
00:11:02,900 --> 00:11:08,480
Ok Allison we want to get rid of Jose because he's clearly not part of this song.

183
00:11:08,780 --> 00:11:09,720
I mean the student.

184
00:11:09,770 --> 00:11:17,520
So we can just say what does he talking about song remember from from grade school.

185
00:11:17,650 --> 00:11:18,390
Jake.

186
00:11:18,530 --> 00:11:21,020
Jingle Hummer's.

187
00:11:21,770 --> 00:11:25,990
Well you're like you guys are like stupid American.

188
00:11:26,180 --> 00:11:26,930
They don't teach that.

189
00:11:26,930 --> 00:11:27,790
Where you're from I get it.

190
00:11:27,830 --> 00:11:29,020
I get it.

191
00:11:29,120 --> 00:11:31,830
What we want to remove Jose because he doesn't belong in the song.

192
00:11:31,850 --> 00:11:33,730
So which index should we get rid of.

193
00:11:33,740 --> 00:11:38,420
Tell me one two three four five or six.

194
00:11:38,420 --> 00:11:39,500
You said two that's right.

195
00:11:39,560 --> 00:11:42,820
Because 2 0 1 2.

196
00:11:42,860 --> 00:11:44,830
So Jose is no longer in our song.

197
00:11:44,930 --> 00:11:48,740
And now if I print the array and say to students

198
00:11:52,490 --> 00:11:53,460
John.

199
00:11:53,560 --> 00:11:55,450
Jean Hammersmith.

200
00:11:55,900 --> 00:12:00,970
OK so lots of fun things you can do with an array and if you want to see all the things that are available

201
00:12:00,970 --> 00:12:07,420
for you when it does command click append here and is going to take us to the documentation and you

202
00:12:07,420 --> 00:12:09,920
can do different things different type of append functions.

203
00:12:10,000 --> 00:12:17,620
Remove the last element insert a new element add an index remove an element add index remove all k a

204
00:12:17,620 --> 00:12:22,120
whole bunch of things you can do with arrays and we've just barely scratched the surface and arrays

205
00:12:22,120 --> 00:12:26,120
are also commonly used hand-in-hand with loops which we haven't talked about yet.

206
00:12:26,130 --> 00:12:27,700
But you'll see that you'll see that to come.

207
00:12:27,700 --> 00:12:31,210
So it's a collection of data allows you to work with different things.

208
00:12:31,210 --> 00:12:36,940
So I think pleura when you think of an array so you know lots of students make an array a list of teachers

209
00:12:36,940 --> 00:12:39,620
make an array a list of anything make an array.

210
00:12:39,940 --> 00:12:46,500
And now you're starting to think of better ways to organize your code rather than doing stuff like this.

211
00:12:46,650 --> 00:12:47,090
OK.

212
00:12:47,200 --> 00:12:50,970
Start using arrays this collection types and that's it.

213
00:12:50,980 --> 00:12:55,990
There's so much more to go into arrays but this is the basic foundational building blocks that I'm hoping

214
00:12:55,980 --> 00:12:56,300
for.

215
00:12:56,300 --> 00:12:57,290
Mark right.

216
00:12:57,370 --> 00:12:59,620
Slopes dot com.

217
00:12:59,740 --> 00:13:01,960
Stay tuned for our exciting next episode.

