1 00:00:00,460 --> 00:00:05,500 Okay, so from our scratch.java, I am happy with how our multidimensional array works. 2 00:00:05,500 --> 00:00:09,940 So I'm gonna copy this and I'm gonna move it into the explore file here. 3 00:00:09,940 --> 00:00:13,020 And let's drop that right here, 4 00:00:13,020 --> 00:00:17,210 underneath this friends array, let's drop our scoreCards in. 5 00:00:17,210 --> 00:00:19,023 I'm gonna highlight these lines. 6 00:00:19,023 --> 00:00:25,191 And if you do Cmd or Ctrl+ bracket, you can indent those to your liking. 7 00:00:25,191 --> 00:00:28,146 I don't know if I like that. 8 00:00:28,146 --> 00:00:29,714 I think that's lined up, there we go. 9 00:00:29,714 --> 00:00:30,840 All right, there we go. 10 00:00:31,920 --> 00:00:36,820 So let's take a moment to plan out what we're about to do. 11 00:00:36,820 --> 00:00:39,860 Actually, before we do that, I'm gonna get rid of this stuff down here. 12 00:00:39,860 --> 00:00:40,990 We don't need this anymore. 13 00:00:40,990 --> 00:00:44,100 Here we go, okay. 14 00:00:44,100 --> 00:00:46,960 So let's take a moment and let's think about what we're gonna do. 15 00:00:47,990 --> 00:00:53,280 Now one thing that I find sorta helpful is to write out your plan in pseudo code, 16 00:00:53,280 --> 00:00:56,650 which means more or less, a code which you can translate later. 17 00:00:56,650 --> 00:01:01,020 Now if you do this in the form of single line comments, some nice magic happens. 18 00:01:01,020 --> 00:01:02,029 So lets think this through. 19 00:01:02,029 --> 00:01:07,969 So let's see, we'll say for each of our friends, so for each friend, 20 00:01:07,969 --> 00:01:13,612 we're gonna loop through them and then we're gonna come in here and 21 00:01:13,612 --> 00:01:17,490 we'll say, we'll print their name, okay? 22 00:01:17,490 --> 00:01:23,230 And then for each of those friends, for each 23 00:01:23,230 --> 00:01:29,003 hole that they've gone through say, print their score. 24 00:01:30,220 --> 00:01:30,870 Make sense, right? 25 00:01:30,870 --> 00:01:35,720 So every friend, print their name for each hole they had will print their score. 26 00:01:35,720 --> 00:01:37,570 So let's see. 27 00:01:37,570 --> 00:01:41,590 We have two arrays, one for our friends and one for 28 00:01:41,590 --> 00:01:44,950 our scoreCards and they just happen to line up right now. 29 00:01:44,950 --> 00:01:46,720 So let's use that to our advantage. 30 00:01:46,720 --> 00:01:53,290 Now, what I mean by line up is that the first score card here is Ben's, right? 31 00:01:53,290 --> 00:01:56,730 And Ben is our first friend. 32 00:01:56,730 --> 00:02:00,840 So in deck zero of friends, represents this friends' scorecard. 33 00:02:00,840 --> 00:02:05,519 And then Alena, is the second, and that is this, so 1 and 2, 0, 34 00:02:05,519 --> 00:02:08,350 1, and 2, they all line up. 35 00:02:08,350 --> 00:02:11,700 So, let's loop through the friends, right? 36 00:02:11,700 --> 00:02:14,930 Let's do this, for each friend let's loop through it. 37 00:02:14,930 --> 00:02:19,432 So, when you need an index, it's best use the standard for loop, right? 38 00:02:19,432 --> 00:02:24,371 Just because we are gonna need this index, so we'll say for the integer i = 0, 39 00:02:24,371 --> 00:02:28,296 that's where we wanna start, we wanna start at the first one. 40 00:02:28,296 --> 00:02:31,695 And as long as it's less than length, 41 00:02:31,695 --> 00:02:36,901 get used to this pattern, that's kind not look as tens and 42 00:02:36,901 --> 00:02:42,019 then we will move this all the way down here to the bottom. 43 00:02:42,019 --> 00:02:46,224 Okay, and now we want to print their name. 44 00:02:46,224 --> 00:02:53,302 So let's say Sytem.out.printf, 45 00:02:53,302 --> 00:02:56,735 we'll do, %s. 46 00:02:56,735 --> 00:02:58,320 Do a new one. 47 00:02:58,320 --> 00:03:00,700 And hey, why don't we also print out a separator? 48 00:03:00,700 --> 00:03:02,680 So we'll just draw something like this. 49 00:03:02,680 --> 00:03:06,580 So something to just mark the screen up kind of nice. 50 00:03:06,580 --> 00:03:09,550 And so there, we wanna get the friends. 51 00:03:09,550 --> 00:03:14,650 So we'll use our index that we have, we'll say friends i, to get that element for 52 00:03:14,650 --> 00:03:17,700 with this current loop through the friends. 53 00:03:17,700 --> 00:03:19,210 Okay, so we can do this. 54 00:03:19,210 --> 00:03:23,440 So now we wanna loop this current friends score for each hole. 55 00:03:23,440 --> 00:03:27,600 So we would show the whole number, so we need to get the index again, so 56 00:03:27,600 --> 00:03:31,482 we'll say, for int, uh-oh. 57 00:03:33,200 --> 00:03:35,530 We're in a nested four loop here and 58 00:03:35,530 --> 00:03:41,120 we already have an i out, it's in scope, right? 59 00:03:41,120 --> 00:03:45,210 So how do we keep these separate in a different for loop, 60 00:03:45,210 --> 00:03:47,900 we can't use i cuz i already exists here. 61 00:03:47,900 --> 00:03:51,860 Well the good news is that this path has already been blazed for us. 62 00:03:51,860 --> 00:03:57,660 This for loop inside of a for loop is called nesting. 63 00:03:57,660 --> 00:04:02,150 Now you know that I really don't love the short variable name of i, but 64 00:04:02,150 --> 00:04:04,772 it is standard practice so we need to get used to it. 65 00:04:04,772 --> 00:04:09,170 Now something I like even less is the common solution for 66 00:04:09,170 --> 00:04:11,970 the inner loop is to use the letter j. 67 00:04:13,350 --> 00:04:15,810 If there was another one, it would be k and 68 00:04:15,810 --> 00:04:19,100 the next one in the nesting structure, etc. 69 00:04:19,100 --> 00:04:23,090 Now this comes from the math world, so we like to put the blame on them. 70 00:04:23,090 --> 00:04:25,090 So we're gonna loop just like this one. 71 00:04:25,090 --> 00:04:30,275 If the j = 0, I'm gonna initialize that, and as long as j is less than, 72 00:04:30,275 --> 00:04:34,865 now we wanna make sure that we go as long as there are holes left for 73 00:04:34,865 --> 00:04:38,440 this specific friend, which is that index i. 74 00:04:38,440 --> 00:04:44,370 So we wanna go scoreCards and we're gonna get this friends row of that. 75 00:04:44,370 --> 00:04:48,522 So that's this row here, we'll say scoreCards 76 00:04:48,522 --> 00:04:53,800 [i].length, which in this case they're all gonna be 18. 77 00:04:53,800 --> 00:04:59,720 And all of those written out there, but we'll be careful, so we'll do this. 78 00:04:59,720 --> 00:05:04,702 And then I'm gonna wrap this, there we go, and it says, print their score. 79 00:05:04,702 --> 00:05:07,280 All right, so we can do that, we've done that before. 80 00:05:07,280 --> 00:05:11,480 So j is now our index into each of these scores. 81 00:05:11,480 --> 00:05:17,196 So we have i for the row and j for the scores. 82 00:05:17,196 --> 00:05:21,435 And that's handy because we need to get a hold of each of these elements, so 83 00:05:21,435 --> 00:05:22,996 let's print their score. 84 00:05:22,996 --> 00:05:29,080 So much like before, let's print out the whole number, 85 00:05:29,080 --> 00:05:34,152 so we'll say, System.out.printf, Hole, 86 00:05:34,152 --> 00:05:39,502 we'll say, %d and then %d %n, for a new line. 87 00:05:40,630 --> 00:05:45,760 And we want to display the whole number first, right? 88 00:05:45,760 --> 00:05:49,160 So that's what that first percent is, so we'll say j, and 89 00:05:49,160 --> 00:05:52,637 remember it's zero based so we need to add the 1, so j + 1. 90 00:05:52,637 --> 00:05:56,351 And now what we want is we want to pull out of that multidimensional array what 91 00:05:56,351 --> 00:05:57,408 we're looking for. 92 00:05:57,408 --> 00:06:01,091 So we are gonna use the outer loops variable of i and 93 00:06:01,091 --> 00:06:03,730 we want the current hole which is j. 94 00:06:07,622 --> 00:06:11,760 Cool, that looks nice and that for loop closes out there. 95 00:06:11,760 --> 00:06:15,020 If you click here, that for loop closes out there, awesome. 96 00:06:16,190 --> 00:06:17,213 What do you say? Let's give it a try. 97 00:06:17,213 --> 00:06:22,553 So we'll say, clear and 98 00:06:22,553 --> 00:06:27,895 javac Explore.java and 99 00:06:27,895 --> 00:06:31,207 java Explore. 100 00:06:31,207 --> 00:06:34,350 I'm gonna pipe that into less just so we can see better. 101 00:06:34,350 --> 00:06:36,892 Here we go. 102 00:06:36,892 --> 00:06:40,623 And there we go, here's bin, here's a nice separator, here's bin scores, 103 00:06:40,623 --> 00:06:43,984 which let's just make sure is right, 1, 2, 4, 2, 6, 5 good. 104 00:06:43,984 --> 00:06:47,670 And if I press space bar it will jump down or if I go down. 105 00:06:47,670 --> 00:06:51,660 So there's Alena, Alena is coming up next and she's got 2, 3, 1, 5. 106 00:06:51,660 --> 00:06:53,210 Awesome, and then there's Pasan scores. 107 00:06:54,400 --> 00:06:55,290 We did it. 108 00:06:55,290 --> 00:06:56,000 There we go. 109 00:06:56,000 --> 00:07:00,480 For each friend, we print out their name, and then for 110 00:07:00,480 --> 00:07:05,240 each hole, we print out their score, just like our pseudo code, which, 111 00:07:05,240 --> 00:07:08,630 by the way, looks like the starts of some great comments. 112 00:07:08,630 --> 00:07:12,780 Now, professionally, you wouldn't wanna leave a comment at this code level because 113 00:07:12,780 --> 00:07:14,420 these are common patterns. 114 00:07:14,420 --> 00:07:15,300 But for now, 115 00:07:15,300 --> 00:07:19,470 comment away, this is a great way to keep track all of that you are learning. 116 00:07:19,470 --> 00:07:21,680 Did that nested loop approach make sense? 117 00:07:21,680 --> 00:07:23,260 If it's still a little murky, 118 00:07:23,260 --> 00:07:26,820 why don't you give that a real watch from when we added a pseudo code? 119 00:07:26,820 --> 00:07:29,600 Now remember, please speed me up if you need to. 120 00:07:29,600 --> 00:07:31,770 If you need to, slow me down. 121 00:07:31,770 --> 00:07:34,910 Now it might sound like I've had a few, just know that I haven't. 122 00:07:34,910 --> 00:07:38,420 But I would like to celebrate with you because you are cruising 123 00:07:38,420 --> 00:07:39,787 through this course. 124 00:07:39,787 --> 00:07:45,090 Multidimensional arrays are hopefully starting to feel a lot less outer spacey. 125 00:07:45,090 --> 00:07:47,970 They're handy for your row and column needs. 126 00:07:47,970 --> 00:07:51,020 You're really starting to see how great arrays can be, and 127 00:07:51,020 --> 00:07:54,320 now I need to show you the bad stuff, and how to deal with it. 128 00:07:54,320 --> 00:07:57,490 Now, arrays have some limitations, and it's time we dealt with them. 129 00:07:57,490 --> 00:07:59,320 Now, don't worry, I'll show you workarounds and 130 00:07:59,320 --> 00:08:02,420 also some more cool tricks that you can do with them, right after this quick break.