‫All right.
‫So now that you know how to use arrays and 2D arrays as well as for each loops, let's combine those
‫let's combine the concepts in one video and one example, but also let's look at nested for loops.
‫So in this video, we're going to go over these different features and then we will see when to use
‫a nested for loop and what the advantages of a nested for loop over a for each loop are when it comes
‫to, for example, multi dimensional arrays.
‫Okay, so let's get started.
‫The first thing that I have here is, as you can see, I have this matrix which is a two dimensional
‫array which has three items towards each dimension, so three rows and three columns, so to speak.
‫So I'm going to use the for each loop by using the for each keyword here this one and tab.
‫And this will then allow me to basically just type in really quickly what type it is, how I want to
‫call the item of the collection that I'm iterating through and the collection name.
‫So this is a neat little trick if you want to become faster coding.
‫So the item will be of type integer because we want to iterate through this matrix which has a bunch
‫of integers in it and the item we can call it item, we could call it num because it's a number, we
‫could call it element.
‫This would be in normal names that you would see here, but item is going to be fine for now.
‫And then the collection that I want to iterate through is going to be my matrix that I prepared up here.
‫And that's also the reason why I needed to make this matrix static, because I'm using it inside of
‫my static main method.
‫And you cannot use non static items or variables inside of the static main method or inside of static
‫methods in general.
‫So in order to iterate forward through this for each loop and display the matrix items, we can just
‫go ahead and use the console right line statement here.
‫So see w double tab and here I'm just going to say display the item and then make an empty space.
‫Or create an empty space and actually let's not use red line, but right.
‫So this will just write it in one line and not create a new line all the time.
‫Now, let's run this real quick.
‫And we will see that we have one, two, three, four, five, six, seven, eight, nine.
‫So all of the items that we have here in our matrix are being displayed.
‫So we're using this for each loop to go through every single item inside of this matrix, and it just
‫goes from left or top left to bottom right, so to speak, in our example here.
‫Okay.
‫So for each loop is a powerful tool.
‫It allows us to go through a collection and a 2D matrix, a 3D matrix, however many dimensions you
‫want to have.
‫All of those are in the end just collections, because here we're using an array and an array is a type
‫of a collection and this is what we needed to give the for each loop to iterate through.
‫So it iterates through our collection matrix.
‫So now we can go ahead and use this in order to show you how you could achieve this using a nested for
‫loop.
‫So the for each loop is super useful if you just want to go through every single item in it, but it
‫has certain disadvantages in terms of certain situations.
‫So in the case that you want to now change the item, you cannot do it because it's protected.
‫So you cannot go ahead and say item should now be overwritten with three, for example.
‫As you can see here.
‫So if you try that it says cannot assign to item because it is a for each iteration variable.
‫So it's not the actual item, it's really just the value storage, so to speak.
‫So we store whatever value we are currently looking at from our collection and what we store it here,
‫the value of it.
‫So in the first iteration, it's one.
‫In the second iteration it's two, but it's never actually the the value itself, it's only a reference
‫to the value.
‫So this really doesn't help us.
‫So if we want to actually adjust it, we would need something like a nested for loop.
‫So here, let's go ahead and create a next for loop.
‫I'm going to add a little bright line statement here saying this is printing our array using a nested
‫for loop.
‫So what is a nested for loop?
‫Well, it's basically a for loop with another for loop inside of it.
‫Okay.
‫So here we're going to use four and we can use double tap again.
‫So we go from I zero to the length.
‫So here we need to enter the length of the iteration.
‫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
‫this would then give me the length of the entire matrix.
‫So basically the number of items that I have inside of this matrix, but that's not what I want.
‫I want to go through each dimension, so I want to go through it row by row and column by column.
‫So the first dimension will be the rows.
‫So I can use a method that arrays offers.
‫So the array class has this get length method where I need to pass the dimension.
‫So this will get me the number represented or just you can see here that represents the number of elements
‫in the specified dimension of the array.
‫So in my case, if I enter zero, this will now return the zero dimension, which basically means the
‫rows.
‫So this will give me the rows.
‫So now if I want to iterate through each individual column, I need to use another for loop in here.
‫So this will be my inner for loop like so inner or loop.
‫And this one out here.
‫This is going to be the outer for loop.
‫Okay.
‫So.
‫What we now need to change, of course, is going to be the iteration variable name.
‫So here we are going to use J instead of I.
‫That is a common practice.
‫So if you have an inner for loop, you usually use J here as to counter variables name.
‫But now we want to compare J not to the length of the first dimension, but of the second dimension.
‫So we enter one here.
‫So this will now allow me to go through every single column.
‫So this for loop job will be to go through the individual columns and this for loops job will go through,
‫the whole will be to go through the individual rows.
‫So that means that we can now go ahead.
‫And just do our right statement right here.
‫But then, of course, we don't have an item here.
‫So what do we have?
‫Well, we have the matrix, the static variable that we created up here.
‫But this matrix, it needs to know which items it should display.
‫So I'm going to say just display I j which means display the F.
‫Row at the J.F. column, which basically means in the first iteration, this I will be zero.
‫And then we start with this for loop and this four loop will go through it until it's done, and then
‫it will only go to the next iteration of the outer for loop.
‫So only then I will be incremented by one.
‫So that means I is going to stay zero until we've gone through this for loop, which means we start
‫at zero for I and then we have J being zero in the first iteration.
‫So we get the first entry, then we have J being one because we incremented once we ran through this
‫line of code and we increase it by one.
‫So now J is going to be one and we say zero one.
‫So the position zero one of our matrix is going to be this two here, then J is going to be two, which
‫will be this three.
‫And then we check is.
‫Two plus one, which is going to be well, j being incremented after the iteration is three less than
‫the length of that current dimension that we're looking at.
‫And the dimension is going to be the column dimension.
‫So how many columns do we have?
‫And in this case we have three columns.
‫So three is not less than three, which is why this followup will be done with its execution.
‫And we go to the outer for loop again.
‫So this time we increase our increment i.
‫By one, which means I will now be one.
‫And then we go through this for loop three times again.
‫So we have I being one and J being zero in the first iteration of this inner for loop.
‫Then it will be one, then it will be two.
‫So it will basically go through this item, then this item, then this item, and each time it will
‫write it onto the console and leave an empty space.
‫And then in the third iteration of our for loop or out of the loop here, it will go through the last
‫row, so to speak.
‫So this here is a nested for.
‫Loop.
‫Okay.
‫So let's run this real quick just to see if this is actually going to do what I'm saying.
‫And you can see here.
‫Well, maybe I should print it slightly differently.
‫Here.
‫I'm going to have another line break.
‫Like.
‫So let's try this again.
‫So you see, this is our 2D array printed using the nested for loop.
‫This is this one here.
‫This one was the for each loop.
‫And now at this point, you might say, hey, what's the advantage, Dennis?
‫Does this even make sense?
‫I don't like what you're doing there.
‫I prefer to use the for each loop.
‫You see, it's super simple, very little bit of code that I required.
‫Well, the cool thing here is I have directly access to the item that I'm currently looking at.
‫So here in this case, I'm just iterating through it.
‫But what if I would go ahead and assign a new value to it?
‫Let's say I assign zero to it.
‫So now if you run this, we will get a bunch of zeros.
‫Here, you see 0000.
‫So now I can create a logic with which I would, for example, overwrite the value, and that is not
‫easily achievable with it for each loop.
‫For each loop is super great.
‫You just display something and run some code for every single item, but not specifically changing the
‫item itself because the item is just a short term storage location for whatever value we have inside
‫of the.
‫Collection that we're looking at, and that's different for a nested loop, and that's already one advantage
‫that you see here.
‫But there are other advantages.
‫And we're going to look at some more examples of nested for loops with this particular example that
‫we have here.
‫We're going to adjust it a little bit in the next video, and you are going to, for example, see how
‫you can get even numbers or odd numbers from your for loop and then also see how you can adjust it so
‫that you only print, for example, diagonal elements.
‫Because this will give you a general idea of what you can use for loops for or nested for, loops for.
‫And basically you have so much freedom in programming and I just wanted to give you some more tools
‫that you can use.
‫So at this point I'd say let's go over to the next video where we are going to look at that.