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
// Brian's
{1, 2, 4, 2, 6, 5, 4, 3, 3, 2, 5, 7, 2, 7, 8, 4, 3, 2}
// Rohald's
{2, 3, 5, 1, 1, 2, 3, 1, 1, 2, 4, 1, 3, 3, 2, 6, 3, 2}
// Laura'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:01
They sound kind of out there, don't they?
0:03
science fiction-y or time travel-y.
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:13
or 2D arrays,
which is really just an array of arrays.
0:15
The arrays that we've been working with
have been one-dimensional.
0:20
2D arrays are super handy at helping
0:23
to describe things in rows and columns.
0:26
If you've used this spreadsheet before,
you can definitely
0:29
imagine this sort of thing.
0:32
In fact, why don't we just start there?
0:33
So here I've created some golf scorecards
using a Google Sheet.
0:37
You can see that
I have three scorecards here
0:40
for each of my friends, Brian,
Rohald, and Laura.
0:42
They were the same ones at the movies.
0:45
We do everything together.
0:47
Each one of these is a row,
and this is a column, right?
0:49
If I want to talk about a specific cell,
I can reference it.
0:53
So this one here is R2,
0:57
and this one here is D2, and C3,
1:01
this is column P and O, right?
1:06
Column B, B8.
1:09
Okay, I'm done, sorry.
1:12
Now as we saw with our Brian scorecard
array, we were able to represent this
1:14
row, Brian's row here, as an array
1:19
But what we're getting at
1:21
is that we want to have these other
two cards in the same array
1:22
We want to make a new array
that has all of this.
1:25
Each entry being an array.
1:28
So let's turn this spreadsheet
into an array.
1:31
In the teacher's notes, I've provided
each of these rows as an array literal.
1:35
I want to show you how to create this,
and then we'll explore it a bit.
1:39
First, let's get into JShell
so we can play with it.
1:42
I don't know about you,
but I make mistakes.
1:45
One nice trick when making multiple line
statements in JShell
1:48
is to create a temporary file
and then have JShell read it.
1:51
Let me show you.
1:55
In our workspace, I'm going to create
a new file called scratch.java.
1:56
We'll use this as a scratchpad of sorts
for whenever
2:01
we want to import things here.
2:03
Let's define our array.
2:06
You just declare a normal integer array
2:08
and remember we want an array of those,
so you just make one more.
2:10
Then we'll name it scorecards.
2:15
We create our first array literal
and now our array is going to create
2:17
arrays of integers.
2:20
Let's get Brian's scorecard
from the teacher's notes.
2:22
I'm going to copy that and paste
Brian's row.
2:25
Add a comma, since that's the first row.
2:30
And then add Rohald's
2:36
add another comma and then Laura's.
2:38
Now if we check back at our spreadsheet
and look at it, we have the same thing.
2:43
So now we have 1242 for Brian
2:47
then 2351 for Rohald, 4421
for Laura. Awesome.
2:50
Now let's get this imported in JShell.
2:56
We have a file here called Scratch.
2:58
Let's save it.
3:00
I'm going to say slash open scratch.java.
3:04
And what that does is it runs this file
and now it's available inside JShell.
3:08
So now
I have a variable called scorecards.
3:14
Here it is. It's
a multi-dimensional array.
3:16
The first dimension is 3
because there are 3 rows,
3:19
and each one of those rows has 18 columns.
3:23
Remember it is really just
an array of arrays, right?
3:26
If I look at scorecards.length,
we'll see that it's 3, the number of rows.
3:29
To get the first row, we just use index 0
3:34
because arrays are zero based.
So we'll say scorecards zero
3:37
and there you go it's the first row.
This was Brian's scorecard that we pasted
3:43
right? So this is Brian,
the first one, the second one here is Rohald.
3:47
Let's see how he did on the fourth hole
3:52
that would be scorecards one to get Rohald
3:55
and then we want to get the fourth hole,
which again, because we're zero based
3:58
would be the third index so we say three.
4:02
A hole-in-one! Nice job Rohald!
Now you do that.
4:06
Show me what Laura got on the 15th hole.
Go ahead and access that
4:10
and print it out in JShell.
Go ahead and pause me.
4:14
Did you get it?
4:18
So Laura happens to be the third row,
which is index 2.
4:19
And I want the 15th hole.
4:23
So I need to subtract 1
from that to get 14.
4:25
Because both of these are zero-based.
4:28
8? Oh, come on.
4:30
Keep practicing.
4:33
You'll get better at this golfing stuff.
4:34
Don't worry, Laura.
4:36
I know. Let's do this.
4:37
Since we have two arrays, the friends
array and the scorecards array,
4:39
why don't we loop and print out
all of their scores?
4:43
Why don't we take a quick break
and let this sink in a bit?
4:46
Now, taking breaks is a super important
habit to get into for learning.
4:49
So take a stretch, get some fresh air.
4:54
While you're out, let your brain wander
about 2D arrays a bit.
4:56
think about how they're
just the spreadsheet here, right?
5:00
Rows and columns.
5:02
Think about how you can access them,
this 8 here was P4.
5:04
To get to Laura, that's 2-14.
5:09
It's very similar.
5:12
One thing I want to point out before
I let you go here,
5:13
you can also add another dimension
to get a 3D array,
5:16
but you have to wear
special glasses to see it.
5:19
Just kidding.
5:23
But seriously,
5:24
you can keep appending them out
as far as you'd like, but it gets weird quick.
5:25
When you come back,
we'll loop over our scorecard.
5:29
Sound good? See you in a bit.
5:31
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