Welcome back in this video, we're going to look at the race and race, as we saw earlier, allow us
to store multiple pieces of data in one variable, so to speak.
So let's create our first rate for the integer type and also see the different ways we can create an
array.
So first of all, I'm going to use the keyword volume and I'm going to call this variable numbers and
that will be of type entry.
OK, so using in-tray and then I need to use this method called int array of where I can then assign
values.
So int values and multiple ones so far are elements of Typekit, which means I can just go ahead and
just add a couple of numbers separated with a comma.
So I have these values here one, two six.
So that's one way of creating it.
But then because of type inference, we can just go ahead and use a simpler approach where we can get
rid of this in-tray here entirely.
So let me copy that.
And here we can get rid of this part.
Therefore, I need to comment this out.
So because we are using int array of numbers knows that it should be an array of integers.
So that's type inference here.
We don't need to specifically state that it's going to be off type into array.
So by the way, there is an even simpler way of doing it.
So another way of using type inference, and I'm going to again copy the line of code comment the other
way out.
And by the way, all of them are valid, right?
These are just more descriptive.
So they are really, say, throwing to the point what's going on here?
And then we have this one where we don't even need to say that it's an entry, which is to say that
it's an array of.
So now let's go ahead and print an array.
We can just go ahead and say print numbers, and let's run this real quick.
Then we will see that it just prints this information for us.
So we would have expected to get the numbers here, right?
But what we're getting is actually this year.
So we are getting the address of the array, which is where it is stored in memory.
This is because we cannot just print a race with the print method.
We will need to use a utility method provided in Scotland for that purpose.
So what we can do is we can use numbers and that's a cool thing of the array off here.
So this is an array now and we can just go ahead and use a method called content.
To string.
So what that will do is it will transfer the content into a string, convert it for us, and then we
can now display the numbers and you can see now it just says, OK, this is an array, and that's why
we have those square brackets here.
One two three four five six.
OK, so now we can see the values in the right format.
So now let's traverse through in a way, meaning go through the items of the array and print them individually.
So that would be a different approach.
Let's look at it.
And I'm going to simplify the code here, and the easiest way to do it is to use a for loop where I'm
just going to say an element inside of numbers.
And this element is just a variable name.
You could have called it I or whatever you want it.
And I'm just going to say the element in numbers is what I want to print.
So let's print elements here.
So what's happening now is that it goes through these numbers array.
And for every single item starting from the 1st, it will store that in element.
In the first iteration and the second iteration, it will store the two in element.
In the third iteration, it will store to three an element and so forth.
So it's going to go through all the numbers inside of this numbers array and we'll store them in element
and then print them out and then go to the next number printed out, go to the next number and so forth.
So here we're just printing, but you could do whatever you want to do with that particular element.
You could modify it and store it somewhere else.
You could display it and so forth.
So we're just going to display on the console using a print state and you can see one, two, three,
four, five and six.
So all the numbers that are inside of numbers are now being displayed.
So now let's actually do a modification here, so I'm just going to go ahead and add an empty space
here and add two to it.
So here plus two, for example.
Now, if I run that, I will see that it will just display one plus two, two plus two and so forth,
it's not doing the actual calculation.
Do you recall how to actually do the calculation here when we were using string manipulation here?
What we need to add the curly brackets around it.
So let's run it again.
And now it will add the number to the element.
But that by itself will not change the numbers itself.
OK, so if we were to now do the same thing once again after that and we print out numbers without adding
the two to it, therefore we don't need to have those brackets.
Let's run this code and you will see that our numbers are not actually impacted.
So three four five six seven eight.
But the actual numbers are still one two three four five.
So we didn't change the value itself.
We just displayed it, so we didn't overwrite it.
So now let's modify a value in the array, and since arrays are indexed based on starts counting from
zero, it means a position of one in our array is going to be zero.
The index of two or the position of two will be one and so forth.
It's a little confusing.
But let's look at it in order to understand it a little better.
So I'm going to get rid of this for loop here.
I'm going to print numbers and now if I want to access a particular item inside of the numbers array,
I just need to use the square brackets here.
So if I, for example, want to access index zero, this will give me one.
Let's test this.
And you will see that at this place one, in fact, because it starts at index zero, but the first
entry that we have is one at this position.
So now let's use Index three, for example, and that will give us the four.
Let's run it again.
And here you see four.
So this has nothing to do with the actual numbers that are in here.
It just has to do with the position that we are looking at.
So the index of that array.
So here, let's say I'm entering five and I get the number at position zero.
Now it will give me the five instead of the one because that's what is stored at position zero.
OK, let me go back to one here.
So now let's modify our arrays a little bit.
So now let's modify the array a little bit.
I want to store a different value at position zero, for example, in order to achieve that.
I just use the name of the variable, which is numbers then are used to square brackets with the index,
which I want to change, and then I assign the value that I want to assign.
So in this case, let's say it's position zero.
I want to use the number six and at position one, I want to use the number five and then four index.
I'm going to use the number two and finally four index five.
I'm going to use the one.
OK, so let's now look at what happened, so first of all, I'm going to print the initial value of
my numbers and I'm going to call this one initial.
Values.
And then used to square or the dollar sign with.
The round, the bracket surrounding it, like so and then I'm going to print it with that.
Changed values, so.
With a new line, by the way, therefore, I'm using the backslash end and then going to call it final
values.
OK, so let's run this and see how this has impacted our array here.
We could see initial values were one two three four five six, and the final values were six five three
four two one.
So now, since arrays have a fixed size, we cannot increase the size or try to add a new index since
we have six values and the index starts from zero to five.
If we try to add something to numbers at the index seven, for example, this would not work so I can
get into a bounce out of balance exception doing that.
So let's say I want to assign two to position seven.
Let's run this and you see we get an error here, it says exception in thread main array index out of
bounds exception.
So Index seven out of bounds for length six.
So we try to access an index that doesn't exist.
So the same would even go for Index six because we only have six entries and it starts at zero and goes
all the way to five.
So here exception.
So that main index six out of bounds for length.
Six.
Okay, because it starts at zero.
That's why six is already out of bounds.
Now we can use the same with the double array.
So numbers, for example, this would be our array just using the double array keyword off.
And of course, you could have simplified this the same way.
And here I'm going to override the double array.
Like so but then you have to specifically use double numbers, which means you have to add the floating
point value to them.
Otherwise, it wouldn't work.
So you cannot assign an integer to a double array.
You couldn't see that directly here because we got an error here.
Otherwise.
Or you can go ahead and use an array of four days.
So let's go ahead and use that here.
So while days will be an array of.
And you just use Sun, for example.
Then Monday and you see you separate the entries even if you're using strings each time with a comma.
So each entry is handed like this inside.
Of our brackets.
So then if you want to display it, you would do the same thing as we saw earlier with content string.
So here I have all of the days of the week and now I'm printing them and.
This will have not any impact here.
Let's also get rid of this statement here, and let's just display the days of the week and you see
Sunday, Monday, Tuesday, Wednesday and so forth.
We can even go as far as to store entire objects inside of an array.
So therefore, I'm going to create a small data class outside of this function, also outside of our
main function here at the bottom, and I'm going to call this one fruit.
And this fruit will need a name.
So while name of type string and it will need a price, for example, of type double.
OK, so that's how the structure of our data class will be.
And now we can just go ahead and create a fruits list, which will be an array of.
The different fruits.
So here we can now create fruit objects of fruit object one will be an apple and it has a price of,
let's say, a price of two point five dollars or euros and then create another fruit.
Which will be, for example, great and that will then cost three point five euros per kilo just as
an example.
And we can print the fruits in the same way as we would with the days.
So here this is print fruits with content to string, and let's run it real quick.
And we can see here we have the fruit named Apple Price two point five and the other fruit grape and
so forth.
This will be super useful and we're going to use that quite a bit in the future.
Now you can also access individual items.
Of that fruit and printed instead of printing all the fruits content, you can just go ahead and just
display the fruits name.
OK, so I could just go ahead and say where the fruit is at which index.
So here let me use the quotation marks a rounding.
Basically, I'm saying fruits.
Give me the index as well.
And in order to get the index, we of course need to.
There's a for loop, so for index in roots and then indices.
This gives me the indexes like so.
Then inside I would just to print index, so this doesn't give me the fruit itself, but it gives me
the index of the position of the fruit.
So here I want to get the name of the fruit at a particular index.
That would be a weight.
And.
Let me add the quotation here as well.
This is one way to get the indexes, but if you just want to get the fruits, there is an easier way.
You can just use a follow-up where you go for each individual fruit inside of fruits and you just print
the fruit name.
So here you can just go ahead and say fruit dot name.
And this will give you the fruit name.
So let's run it.
And you will see that we now get the fruit names.
And let me actually get rid of the base here so we can see a little better.
And we could see Apple and grape, and then Apple is at index zero.
Rape is index one.
So this was apple and grape, and this was the indices.
By the way, you can also have an array which uses multiple different types of data, so here let's
use this days, for example, and instead of days now we have this little mix up where we use Tuesday
and then we just assign a numbers like, so it's just a new number.
And then here we also have a fruit object so we can just go ahead and say fruit apple with a two point
five price.
So this would be in a taste, but mix and then this print those as well.
So here mix that content to string.
And therefore, let me comment those other parts out, and let's run our quote.
So you see, we have Sunday, Monday, Tuesday, one to three fruit with the name Apple and price of
two point five.
So the beauty about resolve is really that it can hold different types of data.
And we saw earlier that there are these double arrays, for example, they can only hold doubles and
interest.
It can only hold ends.
There will be more efficient as well as safer.
So why would you use an interface?
You can just go ahead and use an array of?
Well, the idea is that interface are super on point and they can only hold numbers.
So imagine you get the data from somewhere else.
And for some reason, the database gave you an integer the position where you would have expected an
end and then your coat would directly figure it out and you could get away around it.
Except you could add an exception that would then manage that problem and go about it.
OK, and that wouldn't be recognized if you have such a mixed array that doesn't care what kind of data
it has.
OK, so that's pretty much it for this video.
See you in the next one.