And the structure I would like to talk about race, and first I would like to start with a problem that
can be solved with them.
So let's just say we have a car, we travel to another country, and we would like to calculate the
average fuel consumption.
We have the data for every 100 kilometers or miles.
If the destination is close, for example, 200 kilometers away, we can solve this by using two variables.
You add them up and divide them by two.
Easy.
Let's just say the destination is 500 kilometers away.
OK, five variables.
We are still good in what you're going to do when you have to travel 5000 kilometres, you should define
50 variables to store that data.
That's when a race can be used.
They are like containers.
You can store a lot of data in them.
OK, so what arrays are by definition, they are used to store multiple values with the same data type
in a single variable.
That data type can be anything a primitive, a built in object or a custom object that you created.
They can be numbers like fuel consumption, lottery numbers or text like features of a car or movie
titles.
But as I said, they store objects like a lot of cars or movies with all the information that belongs
to them.
Now, let's see what we are going to cover in this lecture.
First, I will show you how to create an array, then what default values are assigned to the elements
in an array after creation.
Right after that, I will explain array indexing how to use it to get the data after that initialization,
then loops for arrays.
And finally, science fiction, the multidimensional arrays.
And as usually there is going to be a coding party.
One last thing I would like to point out here is that we are not going to go very deep in a race and
this lecture, there will be an advanced level race video for that.
All right, let's do this.
We're going to use lottery numbers in this lecture as an example, in my country, we have many lottery
types.
The most popular one is when there are 90 numbers from one to 90 and you have to choose five and you
have to put crosses on them, as you can see on the picture.
So how you can create an array that can store the lottery numbers first you have to define the data
type that will be stored, then square brackets that shows Java.
That's not a variable.
After that, the arrays name lottery numbers then equals new keywords, the data type again and the
size of the array in square brackets.
And finally, a semicolon.
You can also separate the creation and the definition of the size, but you cannot use it until you
don't tell Java was the certain size of the array.
The problem with race is that you cannot increase or decrease their size if you know the certain size
you will need.
They are a very good.
Later in the course, I will show you what you can use instead of array's, but now move on to default
values.
After specifying the size of the array, the array is filled up with default values.
In case of integer, it is going to be zero.
It is the same for other hole.
No data types like both short and long.
In case of float and double, it is going to be zero point zero for boolean.
It is false for Char has a special character, the normal character.
You can see the character code on the right and for object types it is pretty easy.
All object data types default value is no, which means empty object.
Be careful not for objects and no character for char are not the same.
Next is the next thing, so we have an array full of zeros and index is used to select one element of
an array.
The thing starts from zero, not one, like in the case of lottery numbers, zero is the first index
and the last is for OK, how to use indexes.
Let's just say we would like to assign 17 to the first element to do that.
First, the erased name, lottery number, the index of the element we would like to use in square brackets
because then the value 17 and the semicolon.
Now the second one, lottery numbers, one in square brackets equals eighty two and a semicolon to use
the elements of the array.
The formula is almost the same.
So to print out one element with system that out that printer and use the arrays name and the index
in square brackets, you can also assign an array element to another variable.
Just like this.
New numbers value is going to be seventeen plus one eighteen.
You can also override an array.
Elements value the same way as you first assign value to it.
Now, let's continue with initialization, when you initialize, you don't have to tell Java what's
the size of the array because you define the elements in curly braces.
That will make Java define the size.
It can be used when you know the values right at the beginning, and it is very handy because you don't
have to bother with the indexes when you assign values, but there are many cases when you have to calculate
elements later in the code.
Now move on to Loop's for a raise, the easiest way to go through every element of an array is to use
a loop for loop is one of the best.
You start with assigning zero to I, then in the condition I.
It's very important less than the lottery numbers.
And here you can use that length to get the size or length of the array.
And last I plus plus as usually and you can use I as an index, just as you can see in the print and
function, the Falu prints out every element of the array.
There is another type of loop I didn't talk about in the loops lecture.
It is called for each.
It starts with the four key word, but in the parentheses, first you define the data type of the elements
in the array, then the name you would like to refer to in the code, then column and finally the source,
the arrays name in the foreach loop.
You can use the lottery number for one element of the array.
You don't have to bother with indexing or length less than or starting with zero for each is good one.
You don't need to use the index for any other purposes.
You just simply run through the whole array.
Moving on to our last topic in this lecture, the multidimensional race, in case of lottery numbers,
we can store numbers for a week and a simple one dimensional array.
But there are many weeks in a year.
It's around 50 for our array for a year.
Filled up with numbers would look like this.
The indexing starts from zero for both columns and rows.
So how to create such an array for multidimensional arrays?
You have to use two square brackets instead of one.
After the data type, then the arrays name equals then the data type again.
And where you have to tell you about the size of the array, you have to specify both sizes.
The indexing starts from zero, as I said.
So the first element is weekly lottery numbers, square brackets zero, then square brackets zero again
and its value is seventeen.
The last element is weekly lottery numbers, square brackets 53, then four in square brackets and last
multidimensional arrays can be initialized to.
To do that, you have to use different level of curly braces separated by comma.
Be careful with this because it is very easy to miss a comma or put a curly brace to the wrong place.
That's it for now.
We will talk more about race later in the course.
I hope it was clear see in the cording part.