Looping Over 2d Arrays8:02 with Craig Dennis
Let's explore using pseudo code and nested loops to work through our two dimensional array.
Okay, so from our scratch.java, I am happy with how our multidimensional array works. 0:00 So I'm gonna copy this and I'm gonna move it into the explore file here. 0:05 And let's drop that right here, 0:09 underneath this friends array, let's drop our scoreCards in. 0:13 I'm gonna highlight these lines. 0:17 And if you do Cmd or Ctrl+ bracket, you can indent those to your liking. 0:19 I don't know if I like that. 0:25 I think that's lined up, there we go. 0:28 All right, there we go. 0:29 So let's take a moment to plan out what we're about to do. 0:31 Actually, before we do that, I'm gonna get rid of this stuff down here. 0:36 We don't need this anymore. 0:39 Here we go, okay. 0:40 So let's take a moment and let's think about what we're gonna do. 0:44 Now one thing that I find sorta helpful is to write out your plan in pseudo code, 0:47 which means more or less, a code which you can translate later. 0:53 Now if you do this in the form of single line comments, some nice magic happens. 0:56 So lets think this through. 1:01 So let's see, we'll say for each of our friends, so for each friend, 1:02 we're gonna loop through them and then we're gonna come in here and 1:07 we'll say, we'll print their name, okay? 1:13 And then for each of those friends, for each 1:17 hole that they've gone through say, print their score. 1:23 Make sense, right? 1:30 So every friend, print their name for each hole they had will print their score. 1:30 So let's see. 1:35 We have two arrays, one for our friends and one for 1:37 our scoreCards and they just happen to line up right now. 1:41 So let's use that to our advantage. 1:44 Now, what I mean by line up is that the first score card here is Ben's, right? 1:46 And Ben is our first friend. 1:53 So in deck zero of friends, represents this friends' scorecard. 1:56 And then Alena, is the second, and that is this, so 1 and 2, 0, 2:00 1, and 2, they all line up. 2:05 So, let's loop through the friends, right? 2:08 Let's do this, for each friend let's loop through it. 2:11 So, when you need an index, it's best use the standard for loop, right? 2:14 Just because we are gonna need this index, so we'll say for the integer i = 0, 2:19 that's where we wanna start, we wanna start at the first one. 2:24 And as long as it's less than length, 2:28 get used to this pattern, that's kind not look as tens and 2:31 then we will move this all the way down here to the bottom. 2:36 Okay, and now we want to print their name. 2:42 So let's say Sytem.out.printf, 2:46 we'll do, %s. 2:53 Do a new one. 2:56 And hey, why don't we also print out a separator? 2:58 So we'll just draw something like this. 3:00 So something to just mark the screen up kind of nice. 3:02 And so there, we wanna get the friends. 3:06 So we'll use our index that we have, we'll say friends i, to get that element for 3:09 with this current loop through the friends. 3:14 Okay, so we can do this. 3:17 So now we wanna loop this current friends score for each hole. 3:19 So we would show the whole number, so we need to get the index again, so 3:23 we'll say, for int, uh-oh. 3:27 We're in a nested four loop here and 3:33 we already have an i out, it's in scope, right? 3:35 So how do we keep these separate in a different for loop, 3:41 we can't use i cuz i already exists here. 3:45 Well the good news is that this path has already been blazed for us. 3:47 This for loop inside of a for loop is called nesting. 3:51 Now you know that I really don't love the short variable name of i, but 3:57 it is standard practice so we need to get used to it. 4:02 Now something I like even less is the common solution for 4:04 the inner loop is to use the letter j. 4:09 If there was another one, it would be k and 4:13 the next one in the nesting structure, etc. 4:15 Now this comes from the math world, so we like to put the blame on them. 4:19 So we're gonna loop just like this one. 4:23 If the j = 0, I'm gonna initialize that, and as long as j is less than, 4:25 now we wanna make sure that we go as long as there are holes left for 4:30 this specific friend, which is that index i. 4:34 So we wanna go scoreCards and we're gonna get this friends row of that. 4:38 So that's this row here, we'll say scoreCards 4:44 [i].length, which in this case they're all gonna be 18. 4:48 And all of those written out there, but we'll be careful, so we'll do this. 4:53 And then I'm gonna wrap this, there we go, and it says, print their score. 4:59 All right, so we can do that, we've done that before. 5:04 So j is now our index into each of these scores. 5:07 So we have i for the row and j for the scores. 5:11 And that's handy because we need to get a hold of each of these elements, so 5:17 let's print their score. 5:21 So much like before, let's print out the whole number, 5:22 so we'll say, System.out.printf, Hole, 5:29 we'll say, %d and then %d %n, for a new line. 5:34 And we want to display the whole number first, right? 5:40 So that's what that first percent is, so we'll say j, and 5:45 remember it's zero based so we need to add the 1, so j + 1. 5:49 And now what we want is we want to pull out of that multidimensional array what 5:52 we're looking for. 5:56 So we are gonna use the outer loops variable of i and 5:57 we want the current hole which is j. 6:01 Cool, that looks nice and that for loop closes out there. 6:07 If you click here, that for loop closes out there, awesome. 6:11 What do you say? Let's give it a try. 6:16 So we'll say, clear and 6:17 javac Explore.java and 6:22 java Explore. 6:27 I'm gonna pipe that into less just so we can see better. 6:31 Here we go. 6:34 And there we go, here's bin, here's a nice separator, here's bin scores, 6:36 which let's just make sure is right, 1, 2, 4, 2, 6, 5 good. 6:40 And if I press space bar it will jump down or if I go down. 6:43 So there's Alena, Alena is coming up next and she's got 2, 3, 1, 5. 6:47 Awesome, and then there's Pasan scores. 6:51 We did it. 6:54 There we go. 6:55 For each friend, we print out their name, and then for 6:56 each hole, we print out their score, just like our pseudo code, which, 7:00 by the way, looks like the starts of some great comments. 7:05 Now, professionally, you wouldn't wanna leave a comment at this code level because 7:08 these are common patterns. 7:12 But for now, 7:14 comment away, this is a great way to keep track all of that you are learning. 7:15 Did that nested loop approach make sense? 7:19 If it's still a little murky, 7:21 why don't you give that a real watch from when we added a pseudo code? 7:23 Now remember, please speed me up if you need to. 7:26 If you need to, slow me down. 7:29 Now it might sound like I've had a few, just know that I haven't. 7:31 But I would like to celebrate with you because you are cruising 7:34 through this course. 7:38 Multidimensional arrays are hopefully starting to feel a lot less outer spacey. 7:39 They're handy for your row and column needs. 7:45 You're really starting to see how great arrays can be, and 7:47 now I need to show you the bad stuff, and how to deal with it. 7:51 Now, arrays have some limitations, and it's time we dealt with them. 7:54 Now, don't worry, I'll show you workarounds and 7:57 also some more cool tricks that you can do with them, right after this quick break. 7:59
You need to sign up for Treehouse in order to download course files.Sign up