Going to continue on with our discussion of memory and talk about a very important concept called array's
an array is a series of elements of the same type place continuously in memory so to us meaning they're
all next to each other.
Their real use is to store a large sequence of information and we'll be using them a lot in this course.
Here's how do you create an array of type in we'll just call it in tray and knows these square brackets
these kind of mean array and inside the square brackets we're going to have a value as to be a constant
expression like the literals of five in this case or using something with the conc keyword.
This is Max 5.
Let's say Hey this works as well
and you can access each element by using an index.
So if you say array and say we want the very first one will say of 0 this noticer we're using the brackets
again.
So this kind of mean in index of when you kind of just using the variable and when you're declaring
it it just means array K and those that we are you are not starting from is from 1.
We're starting from zero.
So it's something you'll have to get used to when programming in general.
We start from zero not from 1 when we count arrays are the perfect example of this if we wanted to access
the last element of the array we would do this.
So since we know it's 5 it will always be the length the beer a minus one.
So for noticed that the 0 1 2 3 4 5 elements but they're offset by 1 k.
So just notice that if you want the last element of the array it's always the length of the array of
5 in this case minus one.
So this is always the case and 0 2 4 are called the indices of the array k.
So those are the valid indices.
So here is my mental model for arrays and general.
So we have the array name and they're really just a bunch of empty boxes.
It's really how I think of it.
And again they're contiguous so that means they're all laid out next to each other.
They're in a sequence K and up here are all the indices of the array.
So in this case there are an number of boxes.
OK so from 0 to and minus 1 are the indices.
And of course inside are all the value.
So whatever you know in this case for Inter it all be integers say hey.
And those individual boxes are just like any other variables so this injury of 0 is really just an integer
k and they're really just like any other integer variable that we've used before.
So in fact you can think of it this way.
And each index there is a variable of the array type that can be used just like an array just like a
regular variable k.
So as for initialization you can't initialize an array the same way that you can with a regular integer.
Let's say so you have to do it a bit differently.
And here's an example of how we could do it.
So you can have one two three or five all in these kind of curly braces.
K so this law actually set one in the very first position the first index to and the second and three
in the third and so on.
So these are all the values that all have.
So what's Well we can actually do some different initializations using these.
So let's say we had this this is also evaluate an initialize and it will set the first index to have
value 1 and the second to be 2 and the third to be 3.
The fourth will actually be zero and the fifth will be zero as well.
And so actually sort of fill in the values for you and just set them to default so zero and this is
can even do something like this and that will set them all to be zero.
So we're always going to be using this kind of syntax for initializing our array.
So let's see a concrete example of this so let's do something like this.
Let's say we had this do you for us x 8 and 10 as our array values and we had these
and that's just some all of the elements of the areas.
See what we.
Zero knows we're starting from zero.
So these will be the eyes really the index k.
This is why I love for loops is because they operate so well on arrays.
Max gloss and we're just some each one.
So we're going to access the array using each index this will get the I-F element of the array.
So I will go from in this case 0 to 4 and see at some
this this yet 30.
Right.
So that's 10 that's 10 and 10.
So that's the.
So arrays are very powerful and we'll be using them quite a bit in the rest of our projects that we
do.
So in the next lecture I want to talk about their relationships with pointers.