1 00:00:00,300 --> 00:00:00,690 ‫All right. 2 00:00:00,690 --> 00:00:09,750 ‫So now that you know how to use arrays and 2D arrays as well as for each loops, let's combine those 3 00:00:09,750 --> 00:00:17,490 ‫let's combine the concepts in one video and one example, but also let's look at nested for loops. 4 00:00:17,490 --> 00:00:23,100 ‫So in this video, we're going to go over these different features and then we will see when to use 5 00:00:23,100 --> 00:00:28,710 ‫a nested for loop and what the advantages of a nested for loop over a for each loop are when it comes 6 00:00:28,710 --> 00:00:31,410 ‫to, for example, multi dimensional arrays. 7 00:00:31,620 --> 00:00:32,940 ‫Okay, so let's get started. 8 00:00:32,940 --> 00:00:37,410 ‫The first thing that I have here is, as you can see, I have this matrix which is a two dimensional 9 00:00:37,410 --> 00:00:44,010 ‫array which has three items towards each dimension, so three rows and three columns, so to speak. 10 00:00:44,160 --> 00:00:50,910 ‫So I'm going to use the for each loop by using the for each keyword here this one and tab. 11 00:00:50,910 --> 00:00:57,720 ‫And this will then allow me to basically just type in really quickly what type it is, how I want to 12 00:00:57,720 --> 00:01:02,970 ‫call the item of the collection that I'm iterating through and the collection name. 13 00:01:02,970 --> 00:01:07,050 ‫So this is a neat little trick if you want to become faster coding. 14 00:01:07,050 --> 00:01:13,650 ‫So the item will be of type integer because we want to iterate through this matrix which has a bunch 15 00:01:13,650 --> 00:01:20,550 ‫of integers in it and the item we can call it item, we could call it num because it's a number, we 16 00:01:20,550 --> 00:01:22,110 ‫could call it element. 17 00:01:22,110 --> 00:01:26,820 ‫This would be in normal names that you would see here, but item is going to be fine for now. 18 00:01:26,970 --> 00:01:32,130 ‫And then the collection that I want to iterate through is going to be my matrix that I prepared up here. 19 00:01:32,250 --> 00:01:37,020 ‫And that's also the reason why I needed to make this matrix static, because I'm using it inside of 20 00:01:37,020 --> 00:01:38,700 ‫my static main method. 21 00:01:38,700 --> 00:01:46,110 ‫And you cannot use non static items or variables inside of the static main method or inside of static 22 00:01:46,110 --> 00:01:47,190 ‫methods in general. 23 00:01:47,790 --> 00:01:53,520 ‫So in order to iterate forward through this for each loop and display the matrix items, we can just 24 00:01:53,520 --> 00:01:56,670 ‫go ahead and use the console right line statement here. 25 00:01:56,670 --> 00:02:05,760 ‫So see w double tab and here I'm just going to say display the item and then make an empty space. 26 00:02:06,580 --> 00:02:10,180 ‫Or create an empty space and actually let's not use red line, but right. 27 00:02:10,180 --> 00:02:15,280 ‫So this will just write it in one line and not create a new line all the time. 28 00:02:15,550 --> 00:02:16,990 ‫Now, let's run this real quick. 29 00:02:17,990 --> 00:02:22,610 ‫And we will see that we have one, two, three, four, five, six, seven, eight, nine. 30 00:02:22,910 --> 00:02:27,620 ‫So all of the items that we have here in our matrix are being displayed. 31 00:02:27,830 --> 00:02:34,820 ‫So we're using this for each loop to go through every single item inside of this matrix, and it just 32 00:02:34,820 --> 00:02:41,510 ‫goes from left or top left to bottom right, so to speak, in our example here. 33 00:02:42,140 --> 00:02:42,410 ‫Okay. 34 00:02:42,410 --> 00:02:44,830 ‫So for each loop is a powerful tool. 35 00:02:44,840 --> 00:02:51,680 ‫It allows us to go through a collection and a 2D matrix, a 3D matrix, however many dimensions you 36 00:02:51,680 --> 00:02:52,450 ‫want to have. 37 00:02:52,460 --> 00:02:59,840 ‫All of those are in the end just collections, because here we're using an array and an array is a type 38 00:02:59,840 --> 00:03:04,950 ‫of a collection and this is what we needed to give the for each loop to iterate through. 39 00:03:04,970 --> 00:03:08,060 ‫So it iterates through our collection matrix. 40 00:03:08,300 --> 00:03:17,780 ‫So now we can go ahead and use this in order to show you how you could achieve this using a nested for 41 00:03:17,780 --> 00:03:18,140 ‫loop. 42 00:03:18,410 --> 00:03:24,110 ‫So the for each loop is super useful if you just want to go through every single item in it, but it 43 00:03:24,110 --> 00:03:28,490 ‫has certain disadvantages in terms of certain situations. 44 00:03:28,490 --> 00:03:35,390 ‫So in the case that you want to now change the item, you cannot do it because it's protected. 45 00:03:35,390 --> 00:03:40,940 ‫So you cannot go ahead and say item should now be overwritten with three, for example. 46 00:03:40,970 --> 00:03:41,990 ‫As you can see here. 47 00:03:41,990 --> 00:03:49,040 ‫So if you try that it says cannot assign to item because it is a for each iteration variable. 48 00:03:49,400 --> 00:03:56,150 ‫So it's not the actual item, it's really just the value storage, so to speak. 49 00:03:56,150 --> 00:04:03,230 ‫So we store whatever value we are currently looking at from our collection and what we store it here, 50 00:04:03,230 --> 00:04:04,040 ‫the value of it. 51 00:04:04,040 --> 00:04:05,900 ‫So in the first iteration, it's one. 52 00:04:05,900 --> 00:04:11,390 ‫In the second iteration it's two, but it's never actually the the value itself, it's only a reference 53 00:04:11,390 --> 00:04:12,200 ‫to the value. 54 00:04:12,830 --> 00:04:14,700 ‫So this really doesn't help us. 55 00:04:14,720 --> 00:04:21,500 ‫So if we want to actually adjust it, we would need something like a nested for loop. 56 00:04:21,830 --> 00:04:24,890 ‫So here, let's go ahead and create a next for loop. 57 00:04:25,040 --> 00:04:32,150 ‫I'm going to add a little bright line statement here saying this is printing our array using a nested 58 00:04:32,150 --> 00:04:32,570 ‫for loop. 59 00:04:32,570 --> 00:04:34,010 ‫So what is a nested for loop? 60 00:04:34,040 --> 00:04:38,150 ‫Well, it's basically a for loop with another for loop inside of it. 61 00:04:38,180 --> 00:04:38,350 ‫Okay. 62 00:04:38,450 --> 00:04:42,320 ‫So here we're going to use four and we can use double tap again. 63 00:04:42,320 --> 00:04:47,570 ‫So we go from I zero to the length. 64 00:04:47,570 --> 00:04:50,840 ‫So here we need to enter the length of the iteration. 65 00:04:50,840 --> 00:04:57,470 ‫So what I'm going to do here is I'm going to use my matrix, but I could of course use length here and 66 00:04:57,470 --> 00:05:01,250 ‫this would then give me the length of the entire matrix. 67 00:05:01,250 --> 00:05:07,490 ‫So basically the number of items that I have inside of this matrix, but that's not what I want. 68 00:05:07,490 --> 00:05:14,690 ‫I want to go through each dimension, so I want to go through it row by row and column by column. 69 00:05:15,020 --> 00:05:19,610 ‫So the first dimension will be the rows. 70 00:05:19,610 --> 00:05:22,820 ‫So I can use a method that arrays offers. 71 00:05:22,820 --> 00:05:28,250 ‫So the array class has this get length method where I need to pass the dimension. 72 00:05:28,730 --> 00:05:37,370 ‫So this will get me the number represented or just you can see here that represents the number of elements 73 00:05:37,370 --> 00:05:39,950 ‫in the specified dimension of the array. 74 00:05:40,100 --> 00:05:48,740 ‫So in my case, if I enter zero, this will now return the zero dimension, which basically means the 75 00:05:48,740 --> 00:05:49,340 ‫rows. 76 00:05:49,580 --> 00:05:51,170 ‫So this will give me the rows. 77 00:05:51,260 --> 00:05:57,680 ‫So now if I want to iterate through each individual column, I need to use another for loop in here. 78 00:05:57,680 --> 00:06:02,780 ‫So this will be my inner for loop like so inner or loop. 79 00:06:04,990 --> 00:06:06,730 ‫And this one out here. 80 00:06:06,760 --> 00:06:09,610 ‫This is going to be the outer for loop. 81 00:06:10,990 --> 00:06:11,590 ‫Okay. 82 00:06:11,590 --> 00:06:12,370 ‫So. 83 00:06:12,980 --> 00:06:18,770 ‫What we now need to change, of course, is going to be the iteration variable name. 84 00:06:18,770 --> 00:06:21,230 ‫So here we are going to use J instead of I. 85 00:06:21,260 --> 00:06:22,850 ‫That is a common practice. 86 00:06:22,850 --> 00:06:28,310 ‫So if you have an inner for loop, you usually use J here as to counter variables name. 87 00:06:28,610 --> 00:06:36,230 ‫But now we want to compare J not to the length of the first dimension, but of the second dimension. 88 00:06:36,230 --> 00:06:37,760 ‫So we enter one here. 89 00:06:37,880 --> 00:06:41,570 ‫So this will now allow me to go through every single column. 90 00:06:41,570 --> 00:06:48,830 ‫So this for loop job will be to go through the individual columns and this for loops job will go through, 91 00:06:49,010 --> 00:06:52,490 ‫the whole will be to go through the individual rows. 92 00:06:52,910 --> 00:06:56,420 ‫So that means that we can now go ahead. 93 00:06:57,320 --> 00:07:01,220 ‫And just do our right statement right here. 94 00:07:01,310 --> 00:07:03,650 ‫But then, of course, we don't have an item here. 95 00:07:03,650 --> 00:07:04,940 ‫So what do we have? 96 00:07:04,970 --> 00:07:09,950 ‫Well, we have the matrix, the static variable that we created up here. 97 00:07:10,460 --> 00:07:15,770 ‫But this matrix, it needs to know which items it should display. 98 00:07:15,920 --> 00:07:22,070 ‫So I'm going to say just display I j which means display the F. 99 00:07:23,440 --> 00:07:30,340 ‫Row at the J.F. column, which basically means in the first iteration, this I will be zero. 100 00:07:31,060 --> 00:07:35,920 ‫And then we start with this for loop and this four loop will go through it until it's done, and then 101 00:07:35,920 --> 00:07:38,710 ‫it will only go to the next iteration of the outer for loop. 102 00:07:38,710 --> 00:07:42,220 ‫So only then I will be incremented by one. 103 00:07:42,940 --> 00:07:49,120 ‫So that means I is going to stay zero until we've gone through this for loop, which means we start 104 00:07:49,120 --> 00:07:53,590 ‫at zero for I and then we have J being zero in the first iteration. 105 00:07:53,590 --> 00:08:00,610 ‫So we get the first entry, then we have J being one because we incremented once we ran through this 106 00:08:00,610 --> 00:08:04,180 ‫line of code and we increase it by one. 107 00:08:04,180 --> 00:08:09,370 ‫So now J is going to be one and we say zero one. 108 00:08:09,370 --> 00:08:14,950 ‫So the position zero one of our matrix is going to be this two here, then J is going to be two, which 109 00:08:14,950 --> 00:08:16,000 ‫will be this three. 110 00:08:16,000 --> 00:08:17,770 ‫And then we check is. 111 00:08:18,590 --> 00:08:28,430 ‫Two plus one, which is going to be well, j being incremented after the iteration is three less than 112 00:08:28,460 --> 00:08:31,790 ‫the length of that current dimension that we're looking at. 113 00:08:31,790 --> 00:08:34,490 ‫And the dimension is going to be the column dimension. 114 00:08:34,490 --> 00:08:36,140 ‫So how many columns do we have? 115 00:08:36,140 --> 00:08:38,090 ‫And in this case we have three columns. 116 00:08:38,090 --> 00:08:43,400 ‫So three is not less than three, which is why this followup will be done with its execution. 117 00:08:43,400 --> 00:08:46,340 ‫And we go to the outer for loop again. 118 00:08:46,340 --> 00:08:50,360 ‫So this time we increase our increment i. 119 00:08:51,100 --> 00:08:53,650 ‫By one, which means I will now be one. 120 00:08:54,160 --> 00:08:56,830 ‫And then we go through this for loop three times again. 121 00:08:56,830 --> 00:09:02,080 ‫So we have I being one and J being zero in the first iteration of this inner for loop. 122 00:09:02,110 --> 00:09:04,450 ‫Then it will be one, then it will be two. 123 00:09:04,450 --> 00:09:09,130 ‫So it will basically go through this item, then this item, then this item, and each time it will 124 00:09:09,130 --> 00:09:12,130 ‫write it onto the console and leave an empty space. 125 00:09:12,250 --> 00:09:19,120 ‫And then in the third iteration of our for loop or out of the loop here, it will go through the last 126 00:09:19,120 --> 00:09:19,990 ‫row, so to speak. 127 00:09:19,990 --> 00:09:24,460 ‫So this here is a nested for. 128 00:09:25,470 --> 00:09:25,990 ‫Loop. 129 00:09:26,650 --> 00:09:26,880 ‫Okay. 130 00:09:26,940 --> 00:09:31,170 ‫So let's run this real quick just to see if this is actually going to do what I'm saying. 131 00:09:31,170 --> 00:09:32,430 ‫And you can see here. 132 00:09:32,820 --> 00:09:37,050 ‫Well, maybe I should print it slightly differently. 133 00:09:38,380 --> 00:09:38,830 ‫Here. 134 00:09:38,830 --> 00:09:42,310 ‫I'm going to have another line break. 135 00:09:43,130 --> 00:09:43,270 ‫Like. 136 00:09:43,340 --> 00:09:45,410 ‫So let's try this again. 137 00:09:47,110 --> 00:09:52,270 ‫So you see, this is our 2D array printed using the nested for loop. 138 00:09:52,300 --> 00:09:53,530 ‫This is this one here. 139 00:09:53,560 --> 00:09:55,210 ‫This one was the for each loop. 140 00:09:55,300 --> 00:09:58,930 ‫And now at this point, you might say, hey, what's the advantage, Dennis? 141 00:09:59,050 --> 00:10:00,460 ‫Does this even make sense? 142 00:10:00,460 --> 00:10:02,140 ‫I don't like what you're doing there. 143 00:10:02,410 --> 00:10:04,170 ‫I prefer to use the for each loop. 144 00:10:04,180 --> 00:10:07,720 ‫You see, it's super simple, very little bit of code that I required. 145 00:10:07,750 --> 00:10:12,430 ‫Well, the cool thing here is I have directly access to the item that I'm currently looking at. 146 00:10:12,430 --> 00:10:16,270 ‫So here in this case, I'm just iterating through it. 147 00:10:16,270 --> 00:10:20,830 ‫But what if I would go ahead and assign a new value to it? 148 00:10:20,830 --> 00:10:22,630 ‫Let's say I assign zero to it. 149 00:10:23,140 --> 00:10:26,530 ‫So now if you run this, we will get a bunch of zeros. 150 00:10:26,530 --> 00:10:28,750 ‫Here, you see 0000. 151 00:10:28,900 --> 00:10:35,110 ‫So now I can create a logic with which I would, for example, overwrite the value, and that is not 152 00:10:35,110 --> 00:10:37,360 ‫easily achievable with it for each loop. 153 00:10:37,420 --> 00:10:38,700 ‫For each loop is super great. 154 00:10:38,710 --> 00:10:45,040 ‫You just display something and run some code for every single item, but not specifically changing the 155 00:10:45,040 --> 00:10:52,060 ‫item itself because the item is just a short term storage location for whatever value we have inside 156 00:10:52,060 --> 00:10:53,350 ‫of the. 157 00:10:54,200 --> 00:10:59,990 ‫Collection that we're looking at, and that's different for a nested loop, and that's already one advantage 158 00:10:59,990 --> 00:11:00,950 ‫that you see here. 159 00:11:00,950 --> 00:11:02,510 ‫But there are other advantages. 160 00:11:02,510 --> 00:11:07,910 ‫And we're going to look at some more examples of nested for loops with this particular example that 161 00:11:07,910 --> 00:11:08,330 ‫we have here. 162 00:11:08,330 --> 00:11:13,400 ‫We're going to adjust it a little bit in the next video, and you are going to, for example, see how 163 00:11:13,400 --> 00:11:22,520 ‫you can get even numbers or odd numbers from your for loop and then also see how you can adjust it so 164 00:11:22,520 --> 00:11:25,940 ‫that you only print, for example, diagonal elements. 165 00:11:26,210 --> 00:11:33,260 ‫Because this will give you a general idea of what you can use for loops for or nested for, loops for. 166 00:11:33,260 --> 00:11:39,590 ‫And basically you have so much freedom in programming and I just wanted to give you some more tools 167 00:11:39,590 --> 00:11:40,580 ‫that you can use. 168 00:11:41,030 --> 00:11:45,620 ‫So at this point I'd say let's go over to the next video where we are going to look at that.