1 00:00:00,290 --> 00:00:04,180 Let's talk about multi-dimensional arrays. 2 00:00:04,180 --> 00:00:06,320 They sound kinda out there, don't they? 3 00:00:06,320 --> 00:00:08,668 Like science-fictiony or time-travely. 4 00:00:08,668 --> 00:00:12,390 Well, don't let the name make it more difficult than it needs to be. 5 00:00:12,390 --> 00:00:15,170 We're going to explore two-dimensional arrays, 6 00:00:15,170 --> 00:00:20,100 or 2D arrays, which is really just an array of arrays. 7 00:00:20,100 --> 00:00:23,920 Now the arrays that we've been working with have been one-dimensional. 8 00:00:23,920 --> 00:00:29,350 Now 2D arrays are super handy at helping to describe things in rows and columns. 9 00:00:29,350 --> 00:00:33,310 If you've used a spreadsheet before, you can definitely imagine this sort of thing. 10 00:00:33,310 --> 00:00:34,600 In fact, why don't we just start there? 11 00:00:35,680 --> 00:00:39,270 So here I've created some golf score cards using a Google sheet. 12 00:00:39,270 --> 00:00:43,338 And you can see that I have three score cards here for each of my friends, Ben, 13 00:00:43,338 --> 00:00:44,415 Alena, and Pasan. 14 00:00:44,415 --> 00:00:47,990 They were the same ones at the movies, we do everything together. 15 00:00:47,990 --> 00:00:50,470 So these are rows, right? 16 00:00:50,470 --> 00:00:54,330 Each one of these is a row, and this is a column, right? 17 00:00:54,330 --> 00:00:55,940 So this is a column. 18 00:00:55,940 --> 00:01:00,100 And if I wanna talk about a specific one of those cells, I can reference it, right? 19 00:01:00,100 --> 00:01:04,040 So this one here, you can see is B2, and 20 00:01:04,040 --> 00:01:08,847 this one here is B3, and C3, right? 21 00:01:08,847 --> 00:01:14,138 Now as we saw with our Ben score card array, we were able to represent this row, 22 00:01:14,138 --> 00:01:20,630 Ben's row here, we were able to represent this as an array. 23 00:01:20,630 --> 00:01:24,500 But what we're getting at is that we wanna have these other two cards 24 00:01:24,500 --> 00:01:25,510 in the same array. 25 00:01:25,510 --> 00:01:30,748 So now what we wanna do is we wanna make a new array that has all of this, 26 00:01:30,748 --> 00:01:36,260 it's gonna have all of these, each entry is an array. 27 00:01:36,260 --> 00:01:40,900 So, let's turn this spreadsheet into an array. 28 00:01:40,900 --> 00:01:45,170 In the teacher's notes, I've pasted each of these rows as an array literal. 29 00:01:45,170 --> 00:01:48,830 I wanna show you how to create this and then I want to explore it a bit. 30 00:01:48,830 --> 00:01:53,080 So, first let get it into JShell so we can play with it. 31 00:01:53,080 --> 00:01:57,710 But I don't know about you, but I make mistakes. 32 00:01:57,710 --> 00:02:02,149 So, one nice trick when making multiple line statement in JShell just to create 33 00:02:02,149 --> 00:02:04,771 a temporary file and then have JShell read it. 34 00:02:04,771 --> 00:02:06,230 Here, let me show you. 35 00:02:06,230 --> 00:02:10,301 So, in our workspace, I am gonna create a new file, and 36 00:02:10,301 --> 00:02:13,260 I'm just gonna call it scratch.java. 37 00:02:13,260 --> 00:02:16,060 And we'll use this as the scratch pad of sorts, 38 00:02:16,060 --> 00:02:19,670 right, whenever we want to import things here. 39 00:02:19,670 --> 00:02:21,535 So, let's define our array. 40 00:02:21,535 --> 00:02:28,800 You just declare a normal integer array and remember we want an array of those. 41 00:02:28,800 --> 00:02:31,650 So you just make one more. 42 00:02:31,650 --> 00:02:38,897 And then we'll name it scoreCards, so scoreCards. 43 00:02:38,897 --> 00:02:42,310 And then we create our first array literal. 44 00:02:42,310 --> 00:02:45,596 So we'll say that, and this is an array, and 45 00:02:45,596 --> 00:02:49,503 now our array is going to contain arrays of integers. 46 00:02:49,503 --> 00:02:54,380 So, let's copy and past Ben's score card from the teacher's notes. 47 00:02:54,380 --> 00:03:00,303 So I'm gonna do that and paste Ben's note, and I'm gonna add a comma, 48 00:03:00,303 --> 00:03:05,457 cuz that's the first row, and then I'm gonna add Alena's. 49 00:03:10,126 --> 00:03:11,626 And I'm gonna add a comma. 50 00:03:11,626 --> 00:03:15,096 And then I'm going to add Pasan's. 51 00:03:18,142 --> 00:03:22,847 And now if we bring our spreadsheet over here and we look at it, 52 00:03:22,847 --> 00:03:25,030 we have just the same thing. 53 00:03:25,030 --> 00:03:29,762 So we have 1, 2, 4, 2 for Ben, 1, 2, 4, 2 then for Alena, 54 00:03:29,762 --> 00:03:33,475 we have 2, 3, 5, 1 and we have 2, 3, 5, 1. 55 00:03:33,475 --> 00:03:35,900 Cool, and Pasan's 4, 4, 2, 1, 4, 4, 2, 1, awesome. 56 00:03:37,710 --> 00:03:41,520 So now let's get this imported in the JShell. 57 00:03:41,520 --> 00:03:45,115 So we have a file here, it's called scratch server, saved it, 58 00:03:45,115 --> 00:03:49,520 also I'm gonna go ahead, I wanna put a semicolon there as a good citizen. 59 00:03:49,520 --> 00:03:51,399 And I'm gonna run jshell. 60 00:03:54,772 --> 00:03:56,355 Bring this up a bit here. 61 00:03:56,355 --> 00:04:01,521 I'm gonna say /open scratch.java. 62 00:04:04,270 --> 00:04:07,570 And what that does is it runs this file and it's now available. 63 00:04:07,570 --> 00:04:10,840 So now I have a variable called scoreCards. 64 00:04:12,640 --> 00:04:18,184 And you'll see that here it is, it's a multidimensional array, 65 00:04:18,184 --> 00:04:25,115 the first one is 3 cuz there's three rows and each one of the rows has 18 columns. 66 00:04:27,168 --> 00:04:31,659 And remember it really just is an array of arrays, right, so 67 00:04:31,659 --> 00:04:40,330 if I look at scoreCards.length, We'll see that it's 3, that's the number of rows. 68 00:04:40,330 --> 00:04:46,027 So, to get the first row we just use index 0, cuz array is a zero based. 69 00:04:46,027 --> 00:04:50,785 So we'll say score, I'll Ctrl+L here, and 70 00:04:50,785 --> 00:04:55,870 we'll say scoreCards[0], and there we go. 71 00:04:55,870 --> 00:04:57,600 It's the first row. 72 00:04:57,600 --> 00:05:00,100 So this was Ben's score that we pasted, right? 73 00:05:00,100 --> 00:05:01,770 So this is Ben's, this is the first one. 74 00:05:01,770 --> 00:05:05,470 This was Ben, and the second one here is Alena. 75 00:05:07,180 --> 00:05:11,390 So let's see how she did on the fourth hole. 76 00:05:11,390 --> 00:05:15,770 So that would be scoreCards[1] to get Alena. 77 00:05:17,080 --> 00:05:19,790 And then we wanna get the fourth hole, which again, 78 00:05:19,790 --> 00:05:24,620 because we're zero-based here too, would be the third index, right? 79 00:05:24,620 --> 00:05:25,692 So we'd say [3]. 80 00:05:25,692 --> 00:05:29,320 So right cuz it's for zero, one, two, three. 81 00:05:32,100 --> 00:05:36,930 So we get, awesome a hole in one nice job Alena. 82 00:05:36,930 --> 00:05:39,240 So now you do that. 83 00:05:39,240 --> 00:05:48,050 Let's see, I want you to do this as show me what Pasan got on the 15th hole. 84 00:05:48,050 --> 00:05:50,090 Remember this is Pasan here. 85 00:05:50,090 --> 00:05:53,920 So go ahead and access that, and just print it out here in JShell. 86 00:05:53,920 --> 00:05:54,816 Go ahead, pause me. 87 00:05:56,554 --> 00:05:57,632 Did you get it? 88 00:05:57,632 --> 00:06:00,989 So Pasan happens to be 89 00:06:03,702 --> 00:06:08,544 The third row which is index 2 and I want the 15th hole, so I need to subtract 1 90 00:06:08,544 --> 00:06:12,754 from that to get 14 because both of those are zero-based, right? 91 00:06:14,625 --> 00:06:18,605 8, come on buddy, keep practicing man, you'll get better at this golfing, 92 00:06:18,605 --> 00:06:19,779 don't worry Pasan. 93 00:06:19,779 --> 00:06:21,073 I know, let's do this. 94 00:06:21,073 --> 00:06:25,977 Since we have two arrays, the friends array and the scoreCards array, 95 00:06:25,977 --> 00:06:30,100 why don't we loop and print out all their scores? 96 00:06:30,100 --> 00:06:33,100 Why don't we take a quick break and let this sink in a bit. 97 00:06:33,100 --> 00:06:37,650 Now, taking breaks is a super important habit to get into for learning. 98 00:06:37,650 --> 00:06:40,690 So, why don't you take a stretch and get some fresh air. 99 00:06:40,690 --> 00:06:44,780 While you're out, let your brain wonder about 2D arrays a bit. 100 00:06:44,780 --> 00:06:48,150 Think about how they're just like the spreadsheet here, right? 101 00:06:48,150 --> 00:06:51,660 They're just like the spreadsheet with rows and columns. 102 00:06:51,660 --> 00:06:56,977 And think about how you can access them much like, like here is P4, 103 00:06:56,977 --> 00:07:01,008 this 8 was the same that was row 2, 14, right? 104 00:07:01,008 --> 00:07:03,660 For it to get to Pasan, 2, 14. 105 00:07:03,660 --> 00:07:05,189 It's very similar, right? 106 00:07:05,189 --> 00:07:09,850 One thing that I wanna point out before I let you go here, you can also add another 107 00:07:09,850 --> 00:07:14,540 dimension to get a 3D array, but you have to wear glasses to see it. 108 00:07:14,540 --> 00:07:15,630 Just kidding. 109 00:07:15,630 --> 00:07:18,910 But seriously, you can keep appending them out as far as you'd like, but 110 00:07:18,910 --> 00:07:20,830 it gets weird quick. 111 00:07:20,830 --> 00:07:23,920 So when you come back, we'll loop over our score card. 112 00:07:23,920 --> 00:07:24,524 Sound good? 113 00:07:24,524 --> 00:07:25,150 See you in a bit.