 Welcome to Intro to Java with an emphasis on AP Computer Science. A, our topic is two-dimensional arrays. So here's what we'll be looking at. We'll be looking at how to create a 2D array. It's very similar to a 1D array, which we've already done, but it's got one extra D. We'll see that. Talk about how to access the elements, modify the elements. Again, very, very similar to what we've already seen. Talk about initializer lists. It's an interesting format, which you'll see. Talk about how to traverse a 2D array. What's nice is there's just a pattern. It's very simple. Once you memorize it, you've got it memorized. We can do that with a for loop, and a while loop, and an enhanced for loop. The enhanced for loop one's pretty interesting. You'll see that in a little bit. And again, we'll take a look at doing a simple algorithm. We'll look for a maximum value, just like we did before, in a particular 2D array. Let's go ahead and take a look at the code. So to create a 2D array, so first let's remind ourselves what a 1D array. So let's go and say int. I think we did scores equals new, int, and then we had to say how many there were. So that's a one-dimensional. So what we can do with a two-dimensional is we need two of those, and two of those. Let's go ahead and create a three by three. I'm just gonna call this ARR. You'll see that a lot in AP and in Java, just ARR as a placeholder for a general array. And that's how you create a two-dimensional array. So go ahead and compile that and run it. Now we don't see anything, of course, because we didn't print anything. So a couple of things we gotta think about here is this is a three by three array. Now I can make this four, and make it a four by three array that helps you. I usually just use a three by three to keep it simple. So to access an element in the array, we need the row and column. So it's system.out.println and println and say ARR. So I'm gonna say zero and zero. So let's go ahead and compile it and run it. And you can see the value is zero because the default value for integer is zero. Now the question is, if you're trying to visualize this, what we basically created was an array that looks like this. So we have essentially, this is row zero, row one, and row two. This is column zero, column one, and column two. So this is row zero, column zero. This would be row two, column two. This would be row two, column one. So it's just like a little x, y, but it's actually y, x. That's why I'm using a coordinate like column and row, not, or row and column, not y and x because it gets a little confusing. So we usually use the x, y coordinates in that order. So it is what's called row major order. So it is ARR row, and again it's zero indexed and ARR. So we could modify. So let's say we wanted to change this value to, we'll change to one. So we'll say ARR zero, zero equals one. And then we'll make ARR two, two. We'll make that equal to nine for example. And then we can just go ahead and we can print those out. Two, file and execute. And you can see we have our one and nine. So once we've done that, now it looks like this. So you can see how this is kind of like a spreadsheet or a grid or however you wanna visualize that. Now we can also do the same thing that we do with 1D arrays and make an initializer list. But what's interesting about this, if you have to think about this, this is an array of arrays. This is an array, this is a 1D array. This is a 1D array and this is a 1D array, but it's arranged inside of another array. Kind of cool. So if you keep that in mind, becomes kind of obvious what we're gonna do here. So I'm gonna go ahead and do int again. I'm gonna call this board and equals new int. We're doing initializer list. So as I said, this is an array of arrays. So let's go ahead and do one, two, three. That's our first array, four, five, six. And seven, seven comma, oops, comma eight, comma nine. And close that off. So this will give us the following. So one, two, three, four, five, six, seven, eight and nine. So it looks like a little bit of a telephone keypad. So we've now initialized board. Let's go ahead and compile that and make sure that works. So we've initialized the board. I'm not sure what's gonna happen. So I'm kind of curious to see what happens. We actually try to print this. I don't actually know the answer to this one. This is not out.println board. I don't know if it's even possible. Let's find out. Yeah, okay. So I guess it's a 2D array of integers. That's my guess at that memory location. So if you remember from one of the classes, I think writing classes, we talked about the two string method. So here's your two string method for arrays. That's very helpful. So let's go ahead and start traversing. So remember, we're traversing by row and we're traversing by column. So for int, I'm gonna change it from i here to row. I'm gonna change it from i here to row equals zero. Row is less than board.length and then row. And the length is gonna be one, two, and three. Because it contains three arrays. So it's an array of arrays basically. So we have an array inside of an array. We have three arrays inside of a different array. So now we need a nested loop because we're gonna go row and then zero, one, two. Then the next row, zero, one, two. Then the next row, zero, one, two. So I'm gonna do for int call equals zero. And this is where you gotta think really hard. So the call is less than what? In this case, it is board zero.length plus plus. So let me explain that one more time. So this length comes from, this is one array, this is two arrays, this is three arrays. So notice how typically you'll see most of them have the same number of columns. So what we're counting on that actually in this case. So what we've done here is we said board zero.length. This is board zero and the length is three. We could put a row, if we thought for some reason, like this had different dimensions, if this was four, five, six, seven, and then we would do this, but typically you won't see that. So if it's easier to think just board zero, you can do it that way, or you can do board with row. It's either way. So now, just like we did before, we need to pull out one value. So that's what it calls value equals board. I can print this out. So system.out.println value plus quote, space, quote. I'm actually gonna print, not println here. And then system.out.println to leave a blank space there All right, so let's go ahead and try that. Compile it, run it. And you can see we got one, two, three, four, five, six, seven, eight, nine. Because row starts at zero, then we go into column zero. We print this value plus a space. Come back around in here, row is still zero, column becomes one, print that, come back around, row is still zero, column is now two. We print that, this loop is now over. We print ln, come down to here, and we restart the process. Pretty, pretty cool. If you ask me, pretty cool. We can do the same thing with a while loop. And we do int row equals zero, int call equals zero. So we say while row is less than board.length, same thing. While call is less than board zero.length, we need to, then we can just copy this part. It's the same. Copy this part, because it's the same system out print line. And we wanna go to the next line. And then be careful, because you don't wanna forget call plus plus. You don't wanna print, forget row plus plus. But that's the while loop version of that. Let's test it. One, two, three, what did I do wrong? Is anybody watching at home to see why this is not coming up? Pause for a second, you know you can't pause it. It stopped, but I don't wanna pause. All right, so row equals zero, call equals zero, row is less than board length. There's no error, print in here in a system.out.println. So what I'm gonna do is I'm gonna print row plus, this is kind of good practice. See when things go wrong, what do you do? You wanna see if it's printing the right, or using the right values. So it says zero zero zero one zero two. So somehow this row plus plus is not working. Now why is that? And change the board length up here, okay, so that was definitely a big boo boo on my part. Do apologize. So long story short, if you remember in the unit about scope, so this particular variable had to be initialized within this outer loops scope. Otherwise what happened was when we got down to length, it didn't reset, it didn't reset as we expected. So yeah, that's kind of weird. That's a very weird one. Oh yeah, I'll let him learn. I'll have to update that way of book. So there you go. Everybody learned something new today. And then let's go ahead and do this with an enhanced for loop. This is kind of interesting, now watch this. For int row, notice it's int with square brackets because what we're doing is we're iterating through, where is it at? Each of these arrays in order. So each row is a one D array. Then for int value in each row, then we just do the same thing. We can go ahead and pull that out there. We wanna print the value. We wanna print our, let's go ahead and compile it. Rows already defined, of course, I know that's gonna happen. So let's go ahead and comment all this out. And there's gotta be a shortcut for this. So you see we got from here. So we're iterating through the board. Remember it is an array of arrays. So we iterate through each row. And for each row, we iterate through each value, which we've done before. And then again, we're printing it out horizontally and then we're printing out a new line at the end. So our final thing is to take a look at doing a maximum value. So it's the exact same thing that we did before. So what we do is I'm gonna go ahead and just copy this. This part never really changes, just what we do with it. So the actual traversal part never changes. So because it's the same thing, so because it's a 2D array, we need each row, we need each column. In this case, we're looking for the max value. So int max value equals, so again, let's pick one of the values. We usually just do go with board zero and zero. So then we do. So int value equals board row columns. It's the same pattern. It's just where does that information come from? So if the value, this part doesn't change, is greater than max value, max value equals value. Then at the end, system.out.println, max value plus, and we get nine because of course, one, two, three, four, five, six, seven, eight, nine is the last value. One thing I would just recommend is to change one of the values and make sure you get what you expect. So let's go ahead and go, say, board one, especially because it's the last value, happens to be the highest, you don't know what's gonna happen there. So board one one equals 11, let's say. Now what I could do here is max value equals board zero, zero, now I can't do the one thing here because we'll skip multiple values. So we do have to suck it up and do that comparison. Okay, so we are seeing max value is 11, which is what we expected. So again, this pattern doesn't really change. We've got a collection, we iterate through all of the items in order, we pull out one piece of information at a time, we do whatever that check is that we wanna do in this case, we're looking for a max value, and then we go through all of them and boom, we print out that result. Okay, that was a tough one. So let's go back. We looked at how to create 2D arrays, how to access the elements, modify them. We looked at initializer lists and we saw that really showed that a 2D array is actually an array of arrays. We talked about traversing 2D arrays in what's called row major order, using for loops, while loops, and a little, little picky thing with the while loop and where that variable was defined. Otherwise we ended up with a scope problem. So that surprised me too, I've thought about that. And then the enhanced for loop also really shows that you're iterating through an array of arrays. That's why you need that little extra square bracket there. And then just to keep that in mind, what are you iterating through? What's the actual structure that you're iterating through? So yeah, I would practice that. I would try doing some of those other algorithms that we did, you saw in the book previously or you saw some of the videos previously and just to practice that over and over again. So oops, wrong one, so thanks for watching. That was a good one.