Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Java Arrays!
You have completed Java Arrays!
Preview
Let's explore two dimensional arrays. An array of arrays. Rows and Columns.
Copy and Paste
// Ben's
{1, 2, 4, 2, 6, 5, 4, 3, 3, 2, 5, 7, 2, 7, 8, 4, 3, 2}
// Alena's
{2, 3, 5, 1, 1, 2, 3, 1, 1, 2, 4, 1, 3, 3, 2, 6, 3, 2}
// Pasan's
{4, 4, 2, 1, 2, 2, 1, 4, 2, 2, 2, 3, 2, 5, 8, 1, 2, 2}
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Let's talk about multi-dimensional arrays.
0:00
They sound kinda out there, don't they?
0:04
Like science-fictiony or time-travely.
0:06
Well, don't let the name make it
more difficult than it needs to be.
0:08
We're going to explore
two-dimensional arrays,
0:12
or 2D arrays,
which is really just an array of arrays.
0:15
Now the arrays that we've been working
with have been one-dimensional.
0:20
Now 2D arrays are super handy at helping
to describe things in rows and columns.
0:23
If you've used a spreadsheet before, you
can definitely imagine this sort of thing.
0:29
In fact, why don't we just start there?
0:33
So here I've created some golf
score cards using a Google sheet.
0:35
And you can see that I have three score
cards here for each of my friends, Ben,
0:39
Alena, and Pasan.
0:43
They were the same ones at the movies,
we do everything together.
0:44
So these are rows, right?
0:47
Each one of these is a row,
and this is a column, right?
0:50
So this is a column.
0:54
And if I wanna talk about a specific one
of those cells, I can reference it, right?
0:55
So this one here, you can see is B2, and
1:00
this one here is B3, and C3, right?
1:04
Now as we saw with our Ben score card
array, we were able to represent this row,
1:08
Ben's row here, we were able
to represent this as an array.
1:14
But what we're getting at is that
we wanna have these other two cards
1:20
in the same array.
1:24
So now what we wanna do is we wanna
make a new array that has all of this,
1:25
it's gonna have all of these,
each entry is an array.
1:30
So, let's turn this
spreadsheet into an array.
1:36
In the teacher's notes, I've pasted
each of these rows as an array literal.
1:40
I wanna show you how to create this and
then I want to explore it a bit.
1:45
So, first let get it into JShell so
we can play with it.
1:48
But I don't know about you,
but I make mistakes.
1:53
So, one nice trick when making multiple
line statement in JShell just to create
1:57
a temporary file and
then have JShell read it.
2:02
Here, let me show you.
2:04
So, in our workspace,
I am gonna create a new file, and
2:06
I'm just gonna call it scratch.java.
2:10
And we'll use this as
the scratch pad of sorts,
2:13
right, whenever we want
to import things here.
2:16
So, let's define our array.
2:19
You just declare a normal integer array
and remember we want an array of those.
2:21
So you just make one more.
2:28
And then we'll name it scoreCards,
so scoreCards.
2:31
And then we create our
first array literal.
2:38
So we'll say that, and
this is an array, and
2:42
now our array is going to
contain arrays of integers.
2:45
So, let's copy and past Ben's score
card from the teacher's notes.
2:49
So I'm gonna do that and paste Ben's note,
and I'm gonna add a comma,
2:54
cuz that's the first row, and
then I'm gonna add Alena's.
3:00
And I'm gonna add a comma.
3:10
And then I'm going to add Pasan's.
3:11
And now if we bring our spreadsheet
over here and we look at it,
3:18
we have just the same thing.
3:22
So we have 1, 2, 4, 2 for Ben,
1, 2, 4, 2 then for Alena,
3:25
we have 2, 3, 5, 1 and we have 2, 3, 5, 1.
3:29
Cool, and Pasan's 4, 4, 2,
1, 4, 4, 2, 1, awesome.
3:33
So now let's get this
imported in the JShell.
3:37
So we have a file here,
it's called scratch server, saved it,
3:41
also I'm gonna go ahead, I wanna put
a semicolon there as a good citizen.
3:45
And I'm gonna run jshell.
3:49
Bring this up a bit here.
3:54
I'm gonna say /open scratch.java.
3:56
And what that does is it runs
this file and it's now available.
4:04
So now I have a variable
called scoreCards.
4:07
And you'll see that here it is,
it's a multidimensional array,
4:12
the first one is 3 cuz there's three rows
and each one of the rows has 18 columns.
4:18
And remember it really just is
an array of arrays, right, so
4:27
if I look at scoreCards.length, We'll see
that it's 3, that's the number of rows.
4:31
So, to get the first row we just use
index 0, cuz array is a zero based.
4:40
So we'll say score, I'll Ctrl+L here, and
4:46
we'll say scoreCards[0], and there we go.
4:50
It's the first row.
4:55
So this was Ben's score that we pasted,
right?
4:57
So this is Ben's, this is the first one.
5:00
This was Ben, and
the second one here is Alena.
5:01
So let's see how she
did on the fourth hole.
5:07
So that would be
scoreCards[1] to get Alena.
5:11
And then we wanna get the fourth hole,
which again,
5:17
because we're zero-based here too,
would be the third index, right?
5:19
So we'd say [3].
5:24
So right cuz it's for
zero, one, two, three.
5:25
So we get,
awesome a hole in one nice job Alena.
5:32
So now you do that.
5:36
Let's see, I want you to do this as
show me what Pasan got on the 15th hole.
5:39
Remember this is Pasan here.
5:48
So go ahead and access that, and
just print it out here in JShell.
5:50
Go ahead, pause me.
5:53
Did you get it?
5:56
So Pasan happens to be
5:57
The third row which is index 2 and I want
the 15th hole, so I need to subtract 1
6:03
from that to get 14 because both
of those are zero-based, right?
6:08
8, come on buddy, keep practicing man,
you'll get better at this golfing,
6:14
don't worry Pasan.
6:18
I know, let's do this.
6:19
Since we have two arrays, the friends
array and the scoreCards array,
6:21
why don't we loop and
print out all their scores?
6:25
Why don't we take a quick break and
let this sink in a bit.
6:30
Now, taking breaks is a super important
habit to get into for learning.
6:33
So, why don't you take a stretch and
get some fresh air.
6:37
While you're out, let your brain
wonder about 2D arrays a bit.
6:40
Think about how they're just like
the spreadsheet here, right?
6:44
They're just like the spreadsheet
with rows and columns.
6:48
And think about how you can access
them much like, like here is P4,
6:51
this 8 was the same that was row 2,
14, right?
6:56
For it to get to Pasan, 2, 14.
7:01
It's very similar, right?
7:03
One thing that I wanna point out before I
let you go here, you can also add another
7:05
dimension to get a 3D array, but
you have to wear glasses to see it.
7:09
Just kidding.
7:14
But seriously, you can keep appending
them out as far as you'd like, but
7:15
it gets weird quick.
7:18
So when you come back,
we'll loop over our score card.
7:20
Sound good?
7:23
See you in a bit.
7:24
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up