Welcome back.
In this video we're going to look at arrays.
And we're going to create an integer array.
We're going to see other arrays later on as well.
It's just important to understand the general idea of arrays.
And therefore I have five int variables here.
So I have int 12345 and each of them are set to zero.
Now the thing is if I now want to iterate through them and get the values from them, it's very complicated
to do that right.
What is easier is if I put all of those numbers into one array of numbers, into a list, so to speak,
of numbers, and then I can just go through every single item of that list.
And therefore we need to understand how to declare arrays.
Now, in order to declare an array, we can go ahead and do the following.
We enter the type, let's say the int type.
In this case we give the array a name.
So you see here it already gives me int new int four.
So what this will do is it will generate four int values for that array.
Let me actually call this one my int array.
And this will be will be a new int of five values.
So what this new keyword does is it just says hey allocate some space for my array of integers.
And how much space?
Well enough space for five integers.
That's what this new keyword int.
And then in brackets five.
So in square brackets five says so what this array is now going to be capable of is holding five ints.
It cannot hold six.
It cannot hold seven.
It can only hold five ints.
That's because that's how we defined our array.
So generally speaking if you want to use an array just add the square brackets after the data type that
you want to use, give the array a name.
Then the equal sign, use the new keyword, the data type again, and then in square brackets how many
items you want to be able to put into that array.
So an array is similar to a list.
We're going to look at lists later on, but it is different in that sense.
So we cannot later on just add more items to that array because we have a predefined amount of items
that can fit into that array.
So how can I now assign a value to that array?
Well, you can go ahead and say my int array and then in square brackets the index.
So the position inside of your array where you want to assign a value.
So here let's say at index zero.
So at the very first entry I'm going to assign the value of let's say five okay.
So now my int array holds five values.
And they are positioned like so 01234 are the positions of the items inside of that array.
And it's called index.
So the indexes are 0123 and four.
So now what I did is I entered five at this position of int array at index zero okay.
So now let me do the same thing but with the second entry.
So with one and let me add 12 in there for example.
Then 13 for the next entry.
And you can see the IDE is already giving me some ideas here.
So I'm just going to use that okay.
So I just added some values.
Any values.
It's really not that important.
You can see the IDE is trying to give me this here.
And now this is going to cause an error okay I want to show you that because if you look at this we
defined only five items inside of our my int array.
We said there's only enough space for five items, not more.
Please don't allocate any more space.
But then what we're trying to do here is we're trying to access an item that is outside of its bounds.
So there are only five indexes.
And we're trying to access this one that is not here.
So the one that would come here but that doesn't exist.
It's not there.
So the values that we now assigned are here.
It's 12.
Here is 1314 and 15.
And let me also make sure that it looks a little nicer like so okay.
And now let me run this code just real quick to show you that I'm actually going to run into this error.
You see, it doesn't even execute anything more than this line of 16.
And then it gets into this error.
So the error that it finds is the index out of range exception.
So index was outside the bounds of the array.
In other programming languages you often hear index out of bounds exception or something like that.
But basically that says that you're trying to access something that does not exist inside of that array.
And that is something that will cause your program to crash.
So this is a no go.
All right.
Just so you know.
Don't do that.
Even if the IDE is trying to force you to basically with recommending to do that.
Okay, so now this array, if you want to access an item of that array, we can just say so c w Console.WriteLine.
And I'm going to say my int array at index zero.
So for example this will give me five.
Okay so there we are.
It says five.
Why does it give me five.
Because at this position of index zero we have stored the value of five.
Now if I do that for three for example then it says 14.
And that's because we have set 14 to be at the index of three.
So here we declared an array.
And here we assigned values to the array okay.
So that is the starting point.
In the next video we're going to look at arrays.
How we can declare them and directly assign values in one line where we don't have to do this big step
here.
So see you in the next video.