 Okay, welcome to Unit 8 of Introduction to AP Computer Science. This particular unit is about 2D or two-dimensional arrays. Now, we've already seen arrays in Unit 6. We've seen array lists in Unit 7, but let's just go back real quick and remind you what an array is. So, I can make an array of integers, for example, and I can say scores, and let's use curly braces, and let's say here are my test scores, 56, 89, 100, had a good day, and maybe a 94. So basically we have a set of numbers that are organized, and they all have some kind of theme in this case, they are scores. So what we're going to be working on today is a two-dimensional array. Now, a two-dimensional array is an array that has multiple dimensions, but basically I think what helps to visualize it is if you think of a grid. So in this case, what you're seeing basically is a row. But what we're looking at, and this is not correct code, is what we're looking at is a row, multiple rows, and this is not how you do it, but you see you had a row and some columns. So let me switch over here to a different program. I have this kind of set up for you. So basically let's say we're looking at a 2D array. We've got a row, we've got another row, we've got another row, we've got three rows, we've got three columns. So what we're looking at is this is basically an array, like we've already done. So this is index zero, that's the column, this is index one, this is index two, because there are three items, we start at zero. Now this would be row one, also notice it starts at zero. And then this also has a zero, a one, and a two. And then this final row has a zero, a one, and a two. So for most of what we're gonna be doing, we're gonna be looking at rows and then columns. So basically we have kind of an x, y grid. But again what we're doing is we would say this is row zero, column zero. Row zero, column one. So it's kind of y, x. So it's a little bit confusing if you think of it that way. But if you think of it as rows and columns, it's pretty straightforward. Now I just put one, two, three, four, five, six, seven, eight, nine in here, just as an example, like a telephone keypad. But this could have been 34, this could have been 98. It doesn't have to be any particular value, especially if we're using, for example in this case, integers. Let's see if I can undo that, okay? So let's go back to Jgrass, which we're using, and it started on a little bit of programming in 2D arrays. Now this is not as long a unit as some, at least the basic concepts. Because we've already done arrays, we spent a lot of time on arrays already. So we're able to apply what we've learned to 2D arrays. So the first thing I wanna do is I want to create a new array. So I'm gonna say, I'm gonna make an array of integers, or 2D array. So normally we do it like that because we just have one row. But now we're gonna have multiple rows, okay? And I'm gonna call my array just ARR, and I say new int, okay? In this case, what I'm gonna do is I'm gonna do a three by three, three by three 2D array. So three rows and three columns. Again, as a reminder, indices start at zero, so it'd be 0, 1, 2, and 0, 1, 2. That's it, that's all we gotta do, it's pretty straightforward. If you print that, if you run that, nothing, we don't see anything. But that's basically how you create a 2D array. Now this would be a blank 2D array. Since it's an integer, each cell, I'm gonna use the terminology cell. Each cell would have a value of zero, because that's our default value for integers. So we move on to 8.2, which is basically traversing a 2D array. And there's also some stuff in there about algorithms. I'll do a little bit of that in this video. And the rest we'll kind of practice, I guess, as we go along. So using the example I had here, I'm gonna go ahead and create this. 1, 2, 3, 4, 5, 6, 7, 8, 9, and let's see if we can do that. So again, there are integers. We have our square brackets. I'm gonna call this board, because I'm thinking of this as something like either a Tecto board or something like that. And I'm gonna go 1, 2, 3, comma, 4, comma, 5, comma, 6, and then 7, comma, 8, comma, Okay, so notice I've got one opening brace, one closing brace, and then I've got three separate rows. 1, 2, and 3. But again, remember, index-wise, this is zero. This is gonna be zero row, this is row 1, row 2. And then this is gonna be column zero, column zero, column zero. Okay, so this is basically setting up what we saw earlier. So we got row 0, 1, 2, and 3. We've got row 1, 4, 5, and 6. Row 2, 7, 8, and 9. So traversing the array. Okay, so we're gonna be using the same basic concepts we did before. So what I'm gonna do is I'm gonna say 4. Int row, as I mentioned earlier, we start at zero. Row is gonna be less than 3 because we've got 0, 1, and 2. There is no 3 because we start with zero indexing as always. Then row plus plus. Okay, that should be semi-colon. Now, this is where it gets a little bit more interesting. We're gonna need to have a nested loop, which we've seen before. And now we're going row by row. So for each row, so I'm gonna do this row first. Now I gotta go through each column in order. So I'm gonna do 4, oops, 4 int call equals zero. Call, again, in this case, is less than 3. And then call plus plus. Okay, so now in here, what I'm just gonna do is I'm just gonna print out the cell. So watch what I do here. System.out.print, I'm gonna use print here. You'll see why in a second. Board, and it's gonna be row, call. And put plus and put a space after each one, okay? So this is how you access a particular cell, okay? So it's the row number, so let's get back to here, make it a little bit easier. So you start with the row number, so in this case, zero, and then you do the column. So zero, zero is one, zero, one is two, zero, two is three, row increments. Now we go to row one, zero, one, two, then we go to row two, zero, one, two. So we're just going in that order. And then after each row is printed, we'll print a blank line. So now what we should see on the screen is the same thing we saw over here. We should end up seeing a one, two, three, four, five, six, seven, eight, nine. Unless we got an error, which that was a while ago, which somebody told me. Let's run it again. Semicolon's expected. Yeah, that's gonna be one of those days, I can see. Okay, and there we go. We need to get the expected output. So we got one, two, three, four, five, six, seven, eight, nine. So we started out, row was zero, column was zero. So that's row zero, column zero. Then column increments, we're still on row zero, column goes to one. Increments, column goes to two. Increments, now we're at three. And we print a blank line, move down, and we do the same thing over again. Okay, so that is one way to traverse a 2D array. Now, in this case, obviously we're going through every single item in order. So we could do the same thing. I'm gonna call this traversing an array with an enhanced for loop. So watch what I do here for, well, let me just, let's take a look at this first. What is this? Okay, thinking about looking at those symbols and that combination of numbers and commas, what exactly is that? If I took that out of there, what would it be? So hopefully you're thinking yourself, well, it is an array of ints. And if you thought that, you would be correct. So what I'm gonna do here, I'm gonna do four ints. Again, this is an array of ints. Each row is an array of ints in an array called board. So in my mind, I'm thinking for each row, which in this case is an array of ints in the board, so for each of these, I wanna do something. So now I've got my row, now what I can do for int value in the row, system.out.print value plus quote, space, okay? And the same thing after each row, I'm gonna print out a blank line. And I should see the exact same thing. So let me run that, make sure it's working. Okay, so we got the same thing, one, two, three, to nine. And one, two, three, four, five, six, seven, eight, and nine. So it is working as expected. So let me just take a look at this one more time. So what we did there was for each row, okay? Print out each value, print a blank line. Next row, print out each value, print a blank line. Each row, print out each value, end of the story. So you can see we can traverse a 2D array in the same way that we would traverse a 1D array or a single dimensional array. So pretty straightforward. We could also do a while loop, I'll leave that as an exercise to the viewer. So basically everything that we did with single dimensional arrays, we can do with 2D arrays just by modifying the code a little bit. Okay, so we had a bunch of standard algorithms that we looked at in both the array unit and in the array list unit. So let's just take one of them and see how we might convert that so that it works in a 2D array. So let's think about finding the maximum value. So if you remember what we did was we took the array. We started with the first value and we said, okay, this is our maximum. Then we traversed the array, we checked every single value and we said, okay, which one is the maximum? We're gonna do the exact same thing here, okay? Because it's the same principle, the algorithm doesn't change. But the way the data is accessed changes a little bit. So I'm gonna go ahead and create an array or 2D arrays of scores. And I'm gonna say, let's see here, I'm just gonna put some scores in there. And we got here, let's say 45, 85, and say 75. And then let's say we got a 97, had a good day there, 23 had a bad day there, and 76, okay? So now notice in this case, it's not even, it's not 3 by 3 or anything like that. It's actually, we've got two rows, so it's a 2 by 3 2D array, okay? Now, if you remember, when we did single dimensional arrays, we just assumed that the maximum value was the first number. It has to be one of the numbers, so we started there. You had to start it somewhere, so we started there. So what we're gonna do here in this case, I'm gonna say int maxValue equals scores 0, 0. It's gotta be some value, so I'm basically gonna take a look at this. I'm gonna say, hey, this is my maxValue for my purposes. And then I'm gonna do the same thing I did before. For int row equals 0, that row, row equals 0. Row in this case is gonna be less than 2. Because we've got one row, which is index 0. And a second row, which is index 1, so we never get the 2. Row plus plus. Now we have the extra dimension we gotta deal with for int call equals 0. Call is less than, in this case, it's gonna be 3. So we've got 0, 1, 2. Call is less than 3. Call plus plus. And here's what we're gonna do. If scores is the same algorithm. Again, we're just accessing the data differently. Row call is greater than maxValue. Then we set the value of maxValue to the newValue. And so this will iterate, or it will traverse, depending how you wanna say it, through all of these values, and it will find the maxValue. So system.out.println, maxValue equals plus maxValue. So let's run it, and we should get 97. Let's see what happens. Okay, there we go, we got maxValue equals 97. So that's basically all I wanna do in this particular video. I didn't wanna go through all of the standard algorithms. This would be a very good exercise for you to practice on your own and go through and see if you can get them all working. Most of them will work similar to this, at least the ones where you're, you know, searching through the entire space of values. But now the ones where you're reversing, let's say you wanna reverse the one and three, the four and six, seven and eight, that might be a bit more of a challenge because you have to think about the row and that sort of thing. Or shifting might be a challenge if you're shifting, you know, around, but for the most part, most of them are very, very similar in that you just need to traverse and iterate through every single value, either to find the number of, even numbers, for example, or to find, you know, the number that are greater than a certain value, whatever your condition might be. Okay, so just to review real quick, to create a 2D array, you need to declare the type, you need to put two of these, two sets of these, and then you need to just set the size. Remember, arrays cannot change in size once they have been initialized. And then we could also pre-populate by doing the same thing we did with single dimensional arrays, but just putting a bunch of them together. And then remember, iterating, we go row and column. Now it is possible to go column row, you can just switch them around, it's not a big deal. But in the case where you're printing, you gotta go row column, or otherwise it's not gonna print out correctly. For our purposes, the AP will be, as far as I know, all examples will be row and column. So we don't really need to worry too much about column row. So that might help simplify what you need to study. We can use enhanced for loops, but you just gotta be really careful here because what you're iterating through, the board isn't values, the board is a set of arrays, single dimensional arrays. That's kind of an interesting structure. And then once we have the row, then we can get the values out. So we have two enhanced for loops there. And then as I mentioned just a few minutes ago, the standard algorithms will all work with the 2D array, but you gotta do a little bit of thinking, especially in some of the ones where you're shifting. So you wanna give that some thought. Anyway, so yeah, hopefully this is something we can practice and will help give you a good head start on that. Okay, good luck.