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 using pseudo code and nested loops to work through our two dimensional array.
This video doesn't have any notes.
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
Okay, so from our scratch.java,
I'm happy with how our multidimensional
0:01
array works, so I'm going to copy this
and move it into the explore file here.
0:05
Let's drop
that right underneath this friends array.
0:10
I'm going to highlight these lines
and tab them over.
0:14
I think that's lined up, there we go.
0:18
And I'm going to get rid of this stuff
down here, we don't need this anymore.
0:20
There we go.
0:27
So let's take a moment
and think about what we're going to do.
0:28
Now one thing I find sort of helpful
is to write out your plan in pseudocode,
0:31
which means more or less a code
which you can translate later.
0:35
If you do this in the form of single line
comments, some nice magic happens.
0:39
So let's think this through.
0:43
We'll say, for each of our friends,
0:46
we're going to loop through them,
0:50
and then we'll print their name.
0:52
Then for each of those friends,
for each hole that they've gone through,
0:55
we'll print their score.
Makes sense, right?
0:59
So every friend, print their name,
for each hole they had, print their score.
1:02
Now we have two arrays: one of our friends
and one of our scorecards,
1:06
and they just happen to line up right now
So let's use that to our advantage.
1:10
And what I mean by lineup
is that the first scorecard here is
1:15
Brian's right? And Brian
is our first friend. So index
1:17
zero of friends represents
this friend's scorecard.
1:21
And then Rohald is the second,
1:24
0, 1 and 2 they all line up.
So let's loop through the friends.
1:25
And when you
1:30
need an index, it's
best to use the standard for loop,
1:31
just because we going to need this index.
So we'll say for
1:34
the integer i equals 0.
1:39
That's where we want to start.
1:42
And go as long as it's less than length.
1:44
Get used to this pattern.
1:49
And then we'll move this
all the way down here to the bottom.
1:51
Now we want to print our name.
1:55
So let's say system.out.printf.
1:57
We'll do %s and a new line.
2:01
And hey,
why don't we also print out a separator?
2:04
So we'll just draw something this.
2:07
Something
just to mark the screen up kind of nice.
2:09
And so there we want to get the friends.
2:14
So we'll use our index that we have.
2:16
So we'll say friends
i to get that element for this current
2:18
loop through the friends.
2:22
So now we want to loop
2:25
through this current friend's
score for each hole.
2:26
We should show the golf hole number,
so we need to get that index again.
2:29
So we'll say for int and uh oh.
2:33
We're in a nested for loop here,
and we already
2:38
have an i variable out
that's in scope, right?
2:40
So how do we keep these separate
in a different for loop?
2:43
We can't use i,
because i already exists here.
2:45
Well the good news is that this path
has already been blazed for us.
2:49
This for loop inside
a for loop is called nesting.
2:53
Now you know that I really don't
love the short variable name of i,
2:57
but it is standard practice so we need to
get used to it. Now something I like even less
3:00
is the common solution for the inner
loop is to use the letter j.
3:05
If there was another one it would be k
and the next one in the nesting structure, etc.
3:09
Now this comes from the math world,
so we to put the blame on them.
3:13
So we're going to loop just this one.
3:19
for j equals 0.
3:21
I'm going to initialize that,
and as long as j is less than,
3:23
now we want to make sure that we go
as long as there are holes left
3:27
for this specific friend,
which is that index i.
3:31
So we want to go scorecards
and we're going to get this friend's
3:34
row out of that.
3:38
We'll say scorecards i.length, which
in this case, they're all going to be 18.
3:39
And then I'm going to wrap this up.
3:45
There we go.
3:47
And it says print their score.
3:48
Alright, so we can do that.
3:50
We've done that before.
3:51
So j is
now our index into each of these scores.
3:53
So we have
i for the row and j for the scores.
3:57
And that's handy because we need to get
a hold of each of these elements.
4:00
So let's print our score.
4:04
Much before,
let's print out the hole number.
4:06
So we'll say system.out.printf hole,
4:08
and we'll say %d
and then %d, %n for a new line.
4:13
We want to display the hole number
first, right?
4:19
So that's what this first percent is.
4:21
So we'll say j,
4:23
and remember it's zero based,
so we need to add the one so j plus one
4:25
And now what is to pull out
of the multi-dimensional array
4:30
what we looking for. So we're going to use
the outer loops variable of i
4:33
and we want the current hole
which is j. Cool.
4:38
That looks nice.
4:42
What do you say? Let's give it a try.
4:43
I'm going to pipe that into less,
just so we can see it
4:46
better, too.
4:48
And there we go.
4:54
Here's Brian. Here's a nice separator.
4:55
Here's Brian's scores.
4:58
And if I press the space bar,
it'll jump down.
5:00
So here's Rohald.
5:03
And then there's Laura's scores.
5:04
We did it!
5:06
There we go.
5:07
For each friend, we print out their name.
5:08
And then for each hole, we print out
their score, just our pseudocode.
5:10
Which, by the way,
looks the starts of some great comments.
5:14
Now, professionally,
you wouldn't want to leave a comment
5:18
at this code level
because these are common patterns.
5:20
But for now, comment away.
5:23
This is a great way
to keep track of all that you're learning.
5:25
Did that nested for loop approach
make sense?
5:28
If it's still a little murky,
5:31
why don't you give that a rewatch
from when we added the pseudocode.
5:32
Now remember, please speed me up
or slow me down if you need to.
5:35
Multidimensional arrays are hopefully
starting to feel a lot less outer spacey.
5:40
They're handy for your row and column
needs.
5:45
You're really starting
to see how great arrays can be,
5:48
and now I need to show you the bad stuff
and how to deal with it.
5:51
Now arrays have some limitations,
and it's time we dealt with them.
5:55
Now, don't worry,
I'll show you workarounds
5:59
and also some more cool tricks
that you can do with them
6:01
right after this quick break.
6:04
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