Nested Loops & 2D Arrays | Java | Tutorial 23

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey welcome to draft Adam II my name is Mike in this tutorial I want to talk to you guys about two separate topics in Java the first thing we're going to talk about are nested loops and a nested loop is a special structure in Java which is basically just a loop inside of a loop so we have two loops and we have this first like high-level loop and then inside of that loop we actually have another loop but I'm gonna show you guys how you can create a nested loop and we'll talk about why they're useful and what situations we can use them in then I also want to talk to you guys about two-dimensional arrays and a two-dimensional array is essentially just an array where every single element inside the array is itself an array and so it's a way that we can define like a sort of a matrix or a grid as a data structure so the first thing I want to show you guys is nested loops though so let's come down here and I'm basically gonna create a nested loop so I want to say for and I'm just gonna make it normal for loop summers a and i is equal to 0 we'll say i is less than 3 actually i want to start i off at 1 just so we can kind of illustrate this a little bit better and i'll say i plus plus now inside of this for loop i'm actually gonna create another for loop so i'm gonna say 4 and this time instead of making a variable called i we have to make a variable called j because we already used i up here so i can't make another variable called i because it'll override this one up here so I'm just gonna say in J is equal to 0 we'll say J is less than like 4 and we'll say J plus plus so now I have this original loop and inside of this loop we have a nested loop so basically what's gonna happen is every time we iterate through this I loop we're gonna completely iterate through the J loop so we'll complete a full looping through of the J loop for every one iteration through the i-league and that's going to make sense in a second so down here I'm just gonna print out the values for I and J on every iteration of this loop so I'm just gonna say system dot out dot print line and we're just gonna print out I&J so I'll just say I is equal to and we'll put I in there and then we can say J is equal to and put J in here and so now I should be printing out values for I and J as we go through this nested loop so I'm gonna hit the play button and I'm gonna run the program and now we'll be able to see what got printed out so over here on the screen and actually I wanna make J start at 1 instead of 0 sorry all right so let's run it again and over here on the screen you can see that we have these corresponding values so you can see we have values for I and values for J so let's look at some of these values it says I is equal to 1 J is equal to 1 I is equal to 1 J is equal to 2 I is equal to 1 J is equal to 3 so you'll notice that the value for I is staying the same right and so on all three of these print outs we're only on the first iteration of the I loop right we're only on like the first time that it's gone through but you'll notice that J is entirely looping through so J goes from 1 to 2 to 3 so for every one iteration of that I loop we completely loop through the entire J loop now you can see we come down here it says I is equal to 2 and just in that iteration we're looping through the entirety of the J loop so that's basically what's happening with these nested for-loops is for every one iteration of this top loop we completely iterate through the bottom loop and nested loops can be really cool and there's actually a few situations where we're gonna want to use them I'm gonna show you guys one right now but first I actually want to talk to you about another thing we can use in Java which is called a two-dimensional array and the two-dimensional array is an array where every element of the array is an array so it's like array within an array and I'm gonna show you guys how we can create one of those so I'm just gonna come up here and the way that we create a two-dimensional array is very to the way that we create a normal array so I'm just gonna say int because we're gonna create an array of integers and then I'm gonna make to open and close square brackets like that and now I want to give this a name so I'm just gonna call this number grid so we're gonna be creating a number grid and there's actually a couple different ways that we can go about making this so one way would be to say equals new int and then make to open and close square brackets like this and inside of these square brackets I'm gonna have to give this some information so I'm gonna have to put some numbers in here and inside this first first box we're defining how many rows we want to have in the array so in other words like how many items do we want to have we could say like 3 and then this box over here is defining how many columns we want to have or in other words like how many elements we want each array inside of the array to have so we could say like 4 but I think this is a little confusing and just because we're just learning two-dimensional arrays I'm gonna create one using this open and close curly brace syntax so inside of these open and closed curly brackets we can put the elements of our array right and if I was creating a normal array I could just do something like this so I could put like some names like put like Jim put like Karen right I'm putting all these different entries inside of this array but with a two-dimensional array every single entry in the array has to be another array so I'm gonna make an entry just like this and this is gonna be a valid entry in our array and so inside here I'm just gonna put some numbers so I'll just say 1 2 3 and then we can make another element which is gonna be another array we can say 4 5 6 and we'll do 1 4 7 8 9 and finally we'll make another entry in here just for 0 so I'll just say 0 and you'll notice that this has fewer elements than these other 3 and that's actually ok and that's one of the benefits of defining the variable ways that we can control all of the individual elements so you'll see like this is the first element in the array but it's an array right this is the second element in the array and it's an array this is the third and then the fourth so I want to show you guys how we can actually access these values so I'm just gonna print out and we'll see we can print out some of these different values I'm gonna say system dot out dot print line and suppose we wanted to access this one right here up at the top left the way I can access that value is by referring to exactly where it is inside of our little matrix here so I can just say number grid and I want to give this to open and close boxes and inside of this first box I want to put the item in the array that I want to access so like I said this was the first item this is the second item this is the third item so I'm gonna put a zero because I want to access this first item in the array remember a razor indexed starting with zero over here I want to put the item inside of this original array that I want to access so this is going to be the zeroth element inside of number grid zero sum to can put zero and now we should be printing out a one on to the screen so you can see over here we get our one suppose I wanted to access this six well I can do something similar so six is in the second list item so we can come down here and we'll put a one and it's in the zero one two index position so we'll just say two and now we should be able to access that six which we do over here so that's basically how we can access any of these elements inside of the two-dimensional array so taking what we learned about two-dimensional arrays and taking what we learned about nested for-loops let's combine our knowledge and we'll write a nested for loop that's capable of printing out all of the elements in the array so it looks just like this so I'm gonna show you guys how to do that down here in our nested for loop I just want to modify a few things the first thing I want to modify is up here in this original loop so instead of having AI equal to zero equal to one I'm gonna have I equal to zero because the array indexes start at zero and instead of saying I is less than three really what I want to do is I want to say I is less than number grid length so this top loop is going to be responsible for looping through all of the rows inside of our array so I'll say that one more time this I loop this top loop is responsible for looping through all of our list items so it's gonna loop through here through here through here and then through here it's gonna loop over each one of those items and then down here in the J loop again I want to set J equal to zero but I want to say J is less than the number of elements inside of each individual array so we're basically just gonna say number grid I dot length so basically what we're saying here is this J loop is actually gonna loop through each individual element of each individual array so the J loop is gonna loop through this this in this whereas the I loop is just looping over this array this array in this array and the J loop loops through each individual element inside of each one of those arrays so hopefully that is a little bit clearer and then down here I'm just gonna print out the element inside of the array so I'm just gonna say number grid I and J so let's just take a look at this and see how see if it works or not and then I'll sort of walk you guys through what we did one more time so I'm gonna run this and actually I want to do one more thing so after we run this J loop I'm just gonna say system dot out dot print line so I'm gonna print a new line and this will just make it a little bit easier for us to see what's happening so and actually one more thing instead of printing a line up here I'm just gonna do a normal print so now when we run this it's gonna look a little better okay so here we are and we're actually printing out all of the elements inside of this two dimension arrey so I'm gonna walk you guys through one more time what we did down here in this nested loop so hopefully you can kind of wrap your mind around it so over here I created this four loop right and this four loop this top four loop is responsible for looping over all of the elements inside the number grid and the elements inside the number grid are all arrays so it's basically just looping through this through this through this and through this it's only going through four times right down here inside of this J loop I'm looping through all of the elements inside each element of the array so the J loop is responsible for looping through like this this and this and then it loops through this this and this and that loops through this this and this and loops through this so the J loop is moving through all the elements inside of each of these individual arrays and the I loop is just looping through the arrays themselves hopefully that makes sense and down here we're printing out I and J so this refers to the row number right refers to like the vertical row number inside that list and this refers to the column number so like each element inside of that list and by formatting it this way we're able to print out each one of those elements and you'll notice here I just used a print line down here in order to like format it a little bit better and I use the print up here again just to sort of format it a little bit better so that's the basics of using nested loops with two-dimensional arrays if this isn't super clear don't get intimidated and this is a sort of a tough concept to kind of wrap your head around all you have to do is just take the example that I gave you in this lesson play around with it tweak it modify it try to break it apart and then really see how it works and eventually you'll just sort of be able to see how this works and you'll really understand what's going on with these nested loops hey thanks for watching if you enjoyed the video please leave a like and subscribe to drop acad to be the first to know when we release new content also we're always looking to improve so if you have any constructive criticism or questions or anything leave a comment below finally if you're enjoying Academy and you want to help us grow head over to draft Adam e-comm forward slash contribute and invest in our future
Info
Channel: Mike Dane
Views: 95,716
Rating: undefined out of 5
Keywords: Programming
Id: w-9ZTeO7q_E
Channel Id: undefined
Length: 13min 14sec (794 seconds)
Published: Sat Oct 21 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.