WEBVTT

00:01.130 --> 00:06.640
Going to continue on with our discussion of memory and talk about a very important concept called array's

00:07.320 --> 00:15.730
an array is a series of elements of the same type place continuously in memory so to us meaning they're

00:15.740 --> 00:17.220
all next to each other.

00:18.540 --> 00:26.000
Their real use is to store a large sequence of information and we'll be using them a lot in this course.

00:26.880 --> 00:39.120
Here's how do you create an array of type in we'll just call it in tray and knows these square brackets

00:39.130 --> 00:49.000
these kind of mean array and inside the square brackets we're going to have a value as to be a constant

00:49.000 --> 00:54.530
expression like the literals of five in this case or using something with the conc keyword.

00:54.640 --> 00:58.100
This is Max 5.

00:58.170 --> 01:02.200
Let's say Hey this works as well

01:05.400 --> 01:13.090
and you can access each element by using an index.

01:13.100 --> 01:25.820
So if you say array and say we want the very first one will say of 0 this noticer we're using the brackets

01:25.820 --> 01:26.750
again.

01:26.780 --> 01:32.930
So this kind of mean in index of when you kind of just using the variable and when you're declaring

01:32.930 --> 01:40.740
it it just means array K and those that we are you are not starting from is from 1.

01:40.800 --> 01:42.590
We're starting from zero.

01:42.720 --> 01:47.520
So it's something you'll have to get used to when programming in general.

01:47.610 --> 01:56.570
We start from zero not from 1 when we count arrays are the perfect example of this if we wanted to access

01:56.570 --> 01:59.600
the last element of the array we would do this.

01:59.600 --> 02:09.220
So since we know it's 5 it will always be the length the beer a minus one.

02:09.220 --> 02:22.160
So for noticed that the 0 1 2 3 4 5 elements but they're offset by 1 k.

02:22.310 --> 02:28.150
So just notice that if you want the last element of the array it's always the length of the array of

02:28.160 --> 02:32.160
5 in this case minus one.

02:32.180 --> 02:41.640
So this is always the case and 0 2 4 are called the indices of the array k.

02:42.020 --> 02:45.210
So those are the valid indices.

02:45.230 --> 02:51.090
So here is my mental model for arrays and general.

02:51.240 --> 02:59.800
So we have the array name and they're really just a bunch of empty boxes.

02:59.880 --> 03:01.460
It's really how I think of it.

03:01.540 --> 03:06.440
And again they're contiguous so that means they're all laid out next to each other.

03:06.440 --> 03:13.590
They're in a sequence K and up here are all the indices of the array.

03:13.600 --> 03:18.860
So in this case there are an number of boxes.

03:18.890 --> 03:23.880
OK so from 0 to and minus 1 are the indices.

03:24.010 --> 03:27.120
And of course inside are all the value.

03:27.190 --> 03:38.910
So whatever you know in this case for Inter it all be integers say hey.

03:39.200 --> 03:50.210
And those individual boxes are just like any other variables so this injury of 0 is really just an integer

03:51.040 --> 03:56.880
k and they're really just like any other integer variable that we've used before.

03:59.950 --> 04:02.850
So in fact you can think of it this way.

04:03.040 --> 04:10.120
And each index there is a variable of the array type that can be used just like an array just like a

04:10.120 --> 04:13.620
regular variable k.

04:13.660 --> 04:23.380
So as for initialization you can't initialize an array the same way that you can with a regular integer.

04:23.410 --> 04:27.290
Let's say so you have to do it a bit differently.

04:27.470 --> 04:29.890
And here's an example of how we could do it.

04:29.980 --> 04:41.730
So you can have one two three or five all in these kind of curly braces.

04:41.930 --> 04:51.600
K so this law actually set one in the very first position the first index to and the second and three

04:51.600 --> 04:53.560
in the third and so on.

04:54.780 --> 04:59.300
So these are all the values that all have.

04:59.330 --> 05:05.850
So what's Well we can actually do some different initializations using these.

05:05.890 --> 05:15.450
So let's say we had this this is also evaluate an initialize and it will set the first index to have

05:15.540 --> 05:20.650
value 1 and the second to be 2 and the third to be 3.

05:20.700 --> 05:25.310
The fourth will actually be zero and the fifth will be zero as well.

05:26.150 --> 05:36.860
And so actually sort of fill in the values for you and just set them to default so zero and this is

05:36.940 --> 05:42.460
can even do something like this and that will set them all to be zero.

05:42.770 --> 05:49.210
So we're always going to be using this kind of syntax for initializing our array.

05:49.220 --> 06:00.920
So let's see a concrete example of this so let's do something like this.

06:01.090 --> 06:18.050
Let's say we had this do you for us x 8 and 10 as our array values and we had these

06:23.300 --> 06:26.730
and that's just some all of the elements of the areas.

06:26.750 --> 06:30.040
See what we.

06:31.450 --> 06:34.570
Zero knows we're starting from zero.

06:34.780 --> 06:39.520
So these will be the eyes really the index k.

06:39.580 --> 06:44.220
This is why I love for loops is because they operate so well on arrays.

06:45.470 --> 06:53.040
Max gloss and we're just some each one.

06:53.080 --> 07:06.900
So we're going to access the array using each index this will get the I-F element of the array.

07:06.970 --> 07:16.140
So I will go from in this case 0 to 4 and see at some

07:24.090 --> 07:29.000
this this yet 30.

07:29.020 --> 07:29.460
Right.

07:29.470 --> 07:33.880
So that's 10 that's 10 and 10.

07:33.910 --> 07:35.170
So that's the.

07:36.790 --> 07:44.260
So arrays are very powerful and we'll be using them quite a bit in the rest of our projects that we

07:44.260 --> 07:44.860
do.

07:45.340 --> 07:50.280
So in the next lecture I want to talk about their relationships with pointers.
