1 00:00:00,700 --> 00:00:04,390 Before we start, did you try solving the workbook yourself? 2 00:00:04,420 --> 00:00:10,180 If not, please click the link in the resources folder and follow the instructions for this workbook. 3 00:00:11,010 --> 00:00:15,210 Welcome to Workbook 6.14, the last workbook of this section. 4 00:00:15,720 --> 00:00:20,760 The first thing I'm going to do is copy this function into our class, because what it will do is return 5 00:00:20,760 --> 00:00:23,610 a random number between zero and 99. 6 00:00:23,700 --> 00:00:27,750 Now we get to write a function that prints the contents of an array. 7 00:00:27,750 --> 00:00:32,530 So the function is called print to the array public static. 8 00:00:32,549 --> 00:00:39,360 It's going to be void and it will expect a2d array that can store int elements called. 9 00:00:40,190 --> 00:00:41,780 Well, I guess just a ray. 10 00:00:45,140 --> 00:00:47,330 So the function uses a nested loop. 11 00:00:47,330 --> 00:00:50,750 The inner loop prints each row in one line. 12 00:00:51,620 --> 00:00:54,890 So four and I is equal to zero. 13 00:00:54,920 --> 00:01:01,220 The outer loop is going to keep running so long as I is smaller than the number of rows inside of the 14 00:01:01,220 --> 00:01:09,230 studio array plus plus array length for a two RD array represents the number of rows that this array 15 00:01:09,260 --> 00:01:14,390 has, and each run of the outer loop is going to run the inner loop to completion. 16 00:01:14,390 --> 00:01:17,480 So here we can say int j is equal to zero. 17 00:01:17,570 --> 00:01:25,640 J is smaller then however many elements there are in row I so row I length. 18 00:01:26,700 --> 00:01:34,470 J plus plus, and the inner loop runs for as many elements as there are in Row II, which means we can 19 00:01:34,470 --> 00:01:43,140 use this print statement to index every single element J in Row II and print it all right. 20 00:01:43,140 --> 00:01:46,680 After the inner loop runs to completion, print a new line. 21 00:01:50,850 --> 00:01:51,870 Very simple. 22 00:01:52,380 --> 00:01:52,650 All right. 23 00:01:52,650 --> 00:01:55,840 And now we call print to dx array for the following array. 24 00:01:55,860 --> 00:01:57,210 Just to test it out. 25 00:01:57,390 --> 00:01:58,470 So here. 26 00:01:59,980 --> 00:02:03,120 Bridge going to say print to the array array. 27 00:02:04,690 --> 00:02:05,800 I'm not just going to run it. 28 00:02:05,800 --> 00:02:07,720 I will visualize the runtime. 29 00:02:11,590 --> 00:02:14,290 So we'll step inside, print to the array. 30 00:02:14,320 --> 00:02:21,310 The array parameter, as you can see, has three rows and ten elements per row. 31 00:02:22,280 --> 00:02:27,180 Which means every run of the outer loop is going to run the inner loop ten times. 32 00:02:27,200 --> 00:02:32,540 So here we start at I equaling zero, which corresponds to row zero. 33 00:02:33,110 --> 00:02:40,300 The inner loop will keep running so long as J is smaller than the number of elements inside of row zero. 34 00:02:40,310 --> 00:02:44,390 So this inner loop is going to run ten times the first time it runs. 35 00:02:44,390 --> 00:02:45,170 IE is zero. 36 00:02:45,170 --> 00:02:52,940 J is zero, printing 48 I is zero, J is one printing 56, I is zero. 37 00:02:52,940 --> 00:02:56,510 J is two once again also printing 56. 38 00:02:57,470 --> 00:03:05,480 i0j is three this time printing 76 and the inner loop is going to keep running for every single element 39 00:03:05,480 --> 00:03:07,580 inside of row zero. 40 00:03:11,790 --> 00:03:16,410 After the inner loop runs to completion, it prints every single element in row zero. 41 00:03:16,410 --> 00:03:18,750 And now we're ready to print a new line. 42 00:03:19,320 --> 00:03:22,940 All right, Now, the outer loop runs a second time. 43 00:03:22,950 --> 00:03:26,790 This time I equals one representing row one. 44 00:03:26,790 --> 00:03:32,110 And once again, every run of the outer loop is going to run the inner loop to completion. 45 00:03:32,130 --> 00:03:38,940 It's going to run so long as J is smaller than the number of elements inside of row one. 46 00:03:38,940 --> 00:03:41,300 So the inner loop will run ten times. 47 00:03:41,310 --> 00:03:44,370 Here we index element zero of row one. 48 00:03:45,290 --> 00:03:48,080 Here we index element to one over oh one. 49 00:03:48,650 --> 00:03:52,400 Here we index element two from row 173. 50 00:03:52,790 --> 00:03:53,660 All right. 51 00:03:53,660 --> 00:03:56,540 And this is just going to run seven more times. 52 00:03:58,990 --> 00:04:03,250 When J equals nine will be the last time that this inner loop runs. 53 00:04:03,400 --> 00:04:09,130 So the inner loop runs to completion, and then we print a new line so that we prepare for the next 54 00:04:09,130 --> 00:04:10,810 run of our outer loop. 55 00:04:11,520 --> 00:04:18,149 Now I equals two representing row two, and then we use the inner loop to print every single element 56 00:04:18,149 --> 00:04:19,110 in row two. 57 00:04:28,850 --> 00:04:33,770 And once the inner loop runs to completion, we print a new line and we are done. 58 00:04:33,770 --> 00:04:34,220 Now. 59 00:04:34,220 --> 00:04:35,690 A-plus plus increases. 60 00:04:35,690 --> 00:04:36,860 AI to three. 61 00:04:36,870 --> 00:04:40,550 Three is not smaller than the number of rows three. 62 00:04:40,550 --> 00:04:43,370 So our outer loop breaks we are done. 63 00:04:43,370 --> 00:04:49,100 Print two DX array is effectively capable of printing any two dx array that we pass in. 64 00:04:49,100 --> 00:04:51,110 This is just beautiful. 65 00:04:51,110 --> 00:04:52,670 So now we can move on. 66 00:04:52,670 --> 00:04:55,160 Delete the array if you achieve the result. 67 00:04:55,160 --> 00:04:56,810 Indeed we did. 68 00:04:58,310 --> 00:05:02,750 Now create a 2D array with 100 rows and ten columns. 69 00:05:02,750 --> 00:05:06,350 By columns I mean ten elements per row. 70 00:05:06,590 --> 00:05:13,520 So we'll just set this equal to a new 2D array that stores ten rows. 71 00:05:13,790 --> 00:05:15,560 Or was it 100 rows? 72 00:05:16,340 --> 00:05:19,400 And ten columns, ten elements per row. 73 00:05:19,760 --> 00:05:25,640 We'll just say Java Sea, random numbers dot Java, Java, random numbers. 74 00:05:26,480 --> 00:05:31,490 It just prints 100 rows and ten elements per row. 75 00:05:31,490 --> 00:05:32,690 Ten columns. 76 00:05:32,960 --> 00:05:39,770 OC use a nested loop to populate the array with random numbers before calling print to the array. 77 00:05:40,100 --> 00:05:42,290 All right, so what we'll do? 78 00:05:43,410 --> 00:05:44,820 This will create a loop. 79 00:05:46,380 --> 00:05:49,230 The outer loop starts and equaling zero. 80 00:05:49,320 --> 00:05:56,340 The outer loop is going to keep running so long as I is smaller than the number of rows is smaller than 81 00:05:56,340 --> 00:06:01,260 array length, which means the outer loop is going to run 100 times. 82 00:06:01,380 --> 00:06:08,250 And every time the outer loop runs, we need to run the inner loop so that we can go through every single 83 00:06:08,250 --> 00:06:09,960 element in that row. 84 00:06:09,960 --> 00:06:17,330 So the inner loop will keep running so long as J is smaller than the current row where iterating through 85 00:06:17,440 --> 00:06:18,390 is length. 86 00:06:21,980 --> 00:06:23,510 J plus plus. 87 00:06:24,660 --> 00:06:31,650 And so now here we can basically index every single row, every single element in each row, and set 88 00:06:31,650 --> 00:06:34,440 that equal to a random number. 89 00:06:35,880 --> 00:06:36,300 All right. 90 00:06:36,300 --> 00:06:41,370 I'm only going to visualize execution for the first two rows, and then I will let the runtime continue 91 00:06:41,370 --> 00:06:42,150 naturally. 92 00:06:45,270 --> 00:06:45,510 All right. 93 00:06:45,510 --> 00:06:50,730 So here we have an array with 100 rows and ten elements per row. 94 00:06:50,760 --> 00:06:53,610 Obviously, we're not going to break point through all of this. 95 00:06:53,970 --> 00:06:58,410 So first I equals zero corresponding to row zero. 96 00:06:58,440 --> 00:07:04,080 Every time the outer loop runs, it runs the inner loop for as many elements as there are in the current 97 00:07:04,080 --> 00:07:04,650 row. 98 00:07:06,230 --> 00:07:08,480 So Jay starts off at zero. 99 00:07:08,510 --> 00:07:13,070 Here we update the element at index zero zero to a random number. 100 00:07:13,550 --> 00:07:17,960 Here we update the element zero one to a random number. 101 00:07:18,500 --> 00:07:22,070 Here we update the element to zero two to a random number. 102 00:07:22,070 --> 00:07:23,780 And this keeps on going. 103 00:07:23,810 --> 00:07:26,390 The inner loop is going to run to completion. 104 00:07:32,850 --> 00:07:37,290 All right, now I equals one corresponding to row one. 105 00:07:37,320 --> 00:07:44,310 This inner loop is going to keep running so long as J is less than the number of elements in row one. 106 00:07:44,310 --> 00:07:51,750 So J starts off at zero, element zero of row one gets updated to a random number, element to one of 107 00:07:51,750 --> 00:07:52,650 row one. 108 00:07:52,650 --> 00:07:58,110 And now the inner loop is going to update every single element inside of row one. 109 00:08:02,430 --> 00:08:04,170 After we update all over oh one. 110 00:08:04,170 --> 00:08:06,240 Now we move to row two. 111 00:08:06,270 --> 00:08:08,460 I'm not going to do all of that. 112 00:08:08,470 --> 00:08:10,440 That will take a long time. 113 00:08:10,440 --> 00:08:12,210 So let's just press continue. 114 00:08:14,890 --> 00:08:16,510 Let's remove this. 115 00:08:17,080 --> 00:08:18,490 Let's remove these breakpoints. 116 00:08:18,490 --> 00:08:19,720 Then press continue. 117 00:08:21,180 --> 00:08:21,600 All right. 118 00:08:21,600 --> 00:08:26,270 Here you can see we've updated every single row, every single element in that row. 119 00:08:26,280 --> 00:08:28,290 Now it's just time to print it. 120 00:08:28,290 --> 00:08:33,450 We've already demonstrated that print to the array can print any to the array that we pass in. 121 00:08:33,450 --> 00:08:35,490 So we'll just step over this. 122 00:08:36,669 --> 00:08:38,169 And beautiful. 123 00:08:39,049 --> 00:08:40,010 As you can see. 124 00:08:40,690 --> 00:08:44,110 We printed a 2D array of random numbers. 125 00:08:44,140 --> 00:08:46,780 I hope you enjoyed this breakpoint session.