We can use two dimensional arrays to store data that come in the form of a table.
Let's imagine you have a list of grades for one student, Harry, Harry, go to 70 to a 74, a 78 and
a 76 in his courses.
Now an array of integers can easily store this list of data, but what if there are more students?
This data isn't a list anymore.
It's a table with three rows.
So it looks like we're going to need three arrays to store each row of data.
OK, sure, that's fine, but what if there are one students?
Are you going to create a hundred a raise?
I sure hope not.
Because when your data comes in the form of a table or a grid, you need to use it to the array, which
would look something like this.
And it's a lot easier to manage one array as opposed to hundreds.
So in this lesson, you're going to learn when to work with two drays.
First things first, create a new file name to erase Java, to erase an inside the class, make the
main method.
So what is it to Deira, a tutera is an array that contains a race.
Two degrees are perfect when your data is in the form of a grid or a table.
Every row in this table is like its own array.
One array can store the first row of data, a second array can store the second row of data, and a
third array can store the third row of data.
But instead of creating many integer arrays to store individual rows, we can bundle this data into
a two Deira.
The studio array is capable of storing every row in this table.
Because this Todora has three rows and each row is like its own array, and we can access each row with
an index that goes from zero to two.
Also each row, each array contains elements with indexes that go from zero to three.
And so in order to create a tutera, you need to write the type of data you're storing with two brackets.
This means we're creating a two tier array of integer values.
The first bracket is the number of rows.
So we're basically creating a table with three rows.
And the second bracket is the number of elements in each row.
In this case, each row of data is going to contain four elements.
And so from a visual standpoint, this code is going to create an array with three rows and four columns
and by default every value is going to start at zero.
Let's try it out inside your glass, create an array with three rows and four columns into grades is
equal to a new integer to Deira.
The first bracket is the number of rows we're going to have three rows, the second bracket is the number
of elements in each row and each row is going to have four elements.
OK, what now?
So we have three rows and each row is basically an array with four elements, we can grab the row or
in other words, the array of values at index zero, one or two.
Now, in our code, we can index each array and print its contents using arrays not to string.
You can print there, right, index zero.
The IRAD Index one and the red index to.
And as expected, each row is an array with four elements, the first array has Harrys grades, the
second one run and the third one her minie.
So I'm going to print some messages to reflect this.
I'll put a tab of space.
Followed by Harry and a Colin.
I'll do the same thing for the next two print statements.
And her mighty.
All right, we're off to a pretty good start.
So how do you update to Dré values, just like before you can update Arae values from their index?
You can access any item in this table with two indexes, if I want to change your mind is great and
charm's the first index is the area we're interested in.
In this case, Hurum fetching the ROE and fetching the array with an index of two.
The second index refers to the element in that array.
In this case, I'm grabbing the value that's inside row two and has an index of three within that row.
So at this information, I can start by updating Harry's grades.
All of Harry's grades are in the first row, row zero, so the first bracket is going to index the array,
the road that we're interested in.
And we can use the second bracket to index a specific element in zero, Harry got a seventy two in potion.
A 74 in herbology.
A 78 and Earthman, see?
And a seventy six and charm's.
I can go ahead and run the code.
And as you can see, this updates every element in the first story.
Now we're going to do the same thing for a run in her money runs.
Grades are in the second row, the row with an index of one.
So once again, we use the first bracket theory we're interested in and then we can use the second bracket
to index a specific element in a row one.
Ron got a 65 and potion.
A 64 in herbology.
A 61 in Earthman, see?
And a 67 and charm's.
And we can replicate this logic for her Chinese grades.
I can run my code.
And everything works out.
Hope you can appreciate that two ideas are perfect when you want to store a table of data instead of
using multiple one dimensional arrays to store each row individually.
We can use that to Deira to store the entire table at once.
I want to introduce a faster way of initializing to dress, because the code is getting a bit long since
we already know what the values are, we can create the array and define the values in one line.
This code is going to create a two tier with three rows of values.
Each row is an array bundled in a 2D array.
So we'll try this out in our code.
Each arrow of data is basically an array, so we need one array to store Harry's grades.
One array to store runs grades.
And another to store her mind is.
Runner code.
And everything works as it should, the code looks a lot better.
OK, so how do we access each value in a tutera?
Once again, we can access each value using the two indexes.
If I want to access her mind is great and Charm's the first inducts gets the array, it gets the row
with all of her grades and the second index gets the grade that we're interested in.
So I'm going to wrap up this lesson by printing all of their grades as part of a string.
Once again, we used the first bracket to specify the road that we were interested in, all of Harry's
grades are in the first row zero and his potions grade can be found in index zero of the first row.
His herbology grade is an index one.
Earthman index to.
And Charm's Index three.
I can do the same thing with Iran, except that all of his grades are in the second row, row one.
And we can wrap up by doing the same thing with her mind greats.
They're all in the third row row to.
And that's perfect, you created an array that stores three rows of data and then you accessed every
value in this table using the indexes.
We always start with the index of the array, followed by an index for the item inside.
In this lesson you learn to work with, to raise a Deira is an array that contains a race, each row
of values is an array bundled in a tiara.
This makes them perfect for storing a table of data.
That's because each array is like a row inside of a table and you can access any value in the table
using the index as the first index fetches the array that we're interested in and the second index gets
the element inside the array.