 Welcome to Intro to AP Computer Science for new AP teachers. The subject of this video is 2D arrays. So if we're going to look at what is a 2D array, you can probably figure that out. How to create a 2D array, look at the coding of that. It's not too complex, it's very similar to 1D arrays. Looking at accessing and modifying the 2D array elements, traversing a 2D array using a for loop and a while loop, or an enhanced for loop, that's where it gets a little bit more tricky. Again, looking at the types of algorithms students might be asked to do, and in introductory exercise, looking at this topic. So what is a 2D array? So just like a 1D array, it's an ordered data structure that has rows and columns. You could use x and y, but because it's in row, it's called row major order, it's actually row columns, typically the order. It's kind of, so it'd be like y, x, which will confuse students. It holds one type of data, either a primitive type or a reference type. It is fixed in size, so once the size is set, it stays that size. And just like with a regular array with 1D, it is randomly accessible, but this time you need both indices, the row and the column. To create a 2D array, we have a very similar pattern. So it's the type, square brackets and square brackets, the name equals new type, however many rows you want, and however many columns you want. So an example would be int square brackets, square brackets, scores equals new int 3, 3. In this case, it's equal size. So in this case, there are nine elements. We have three rows and three columns. The top left element is index 0, 0. Bottom right element is index 2, 2. Notice it's not 3 because we started 0, so 0, 1, 2. Of course, the default value for an array of integers is 0. We can also create arrays that are pre-populated with values. So you can see here that this is an array of arrays. So here's our array, here's row 1, here's row 2, here's row 3. Column 1 or column 0, I should say row 0, row 1, row 2, it's probably a little better. Column 0, column 0, column 0, column 1, column 1, column 1, column 2, column 2, column 2. And this is the result that you would get if you visualized it. So accessing and modifying an element, so you need the index of the row and the index of the column. So for example, the value equals name row call. And modifying, just reverse that, name row call equals value or object if it's not the reference type. The traversal is fairly straightforward. It's just like a 1D, but we have a nested loop. So first we need to iterate through each row, starting at 0, going to the length of the 2D array, and then row plus plus. But inside that, we then need to go column 0, and to get the length we need scores 0. So we use the first row, the length of the first row. Again, this assumes that all the rows have equal length. That isn't always the case, but typically it will be. And again, this will be something that probably there will be an FRQ or free response question. We have something to do with this. So make sure students really understand how to do 2D arrays. And then column plus plus. At this point, we just pull the information out, just like we've been doing. This is that same pattern. So scores row column, score. And then in this case, we're just printing the score. But this is the basic pattern for int row 0 equals. Make sure the students know the difference between you use length here and you use scores or 0 length here. By the same token, we can do this with a while loop. So we have row column while row is less than scores length, while column is less than scores 0 length. Score equals scores row call system out print line score. And don't forget in here, we increment the column. So row 0, column 0, column 1, column 2. Come down row 1, column 0, column 1, column 2. Row 2, column 0, column 1, column 2. Very important to get that down. And we can also use an enhanced for loop. A little bit more complex because it's an array of arrays. So notice here, we have int square brackets that tells us row. Each row is an array. So for each row in scores inside each row, we have a score. So here we're iterating through rows. Here we're iterating through the values in a particular row. So you can also use this pattern as well. It's pretty straightforward. You've got to be careful because you need this square brackets here. And you've got to keep in mind what you're iterating through. And again, back to the standard algorithms. Once you've got that pattern, that for row equals for call equals, everything else is the same. Nothing else changes because just you pull that information out and you treat it exactly the same as you did in all the other examples we saw previously. The only thing is with reversing, typically you'll be reversing the order of the elements in a row. So flipping the grid left and right. If they ask you to do it by column and if they ask you to flip it vertically, then you have to go from row major order to column major order. That's just something to keep in mind. International exercise. So what you might do is write a tic-tac-toe board on the board, play the game against the students, and note the order of the boxes are filled. Then once the game is over, ask the students to write the code to create the board, U strings for X and O, and then write the code that corresponds to the moves that were played. So board 0, let's say, board 1 equals X because X goes into the center square for example. And that kind of gives them an idea. Then later you can actually program the tic-tac-toe game. That's a lot of fun. So I think that's it. I forgot to do a little wrap up at the end, but I think you saw the topics there. We'll call that a win. So thanks for watching and good luck with your class. Take care.