 Welcome to intro to AP computer science for new AP teachers. This video is about arrays So let's go ahead and take a look at what we're gonna learn here today The topics what is an array again? Hopefully I assume you know this, but it's always good to review We'll look at how to create an array in Java, especially for those of you coming from other languages You might not be familiar with the way Java does things talk about accessing and modifying array elements And it's done by index Next topic is a rage reversal. This is to me. This is one of the key core fundamental concepts in Java in at least in the AP it comes up in multiple choice questions It comes up in the free response questions So it's a really important topic I talk about it throughout the course If you've seen the other videos talked about it in the loops section talked about strings This is our first kind of real collection String is kind of a pseudo collection in a way, but this case. It's an actual collection of values a collection of objects Look a little bit at enhanced for loops, but we won't talk about those until Probably until later. I think Actually, we did talk about it here And then algorithms some just different types of algorithms as soon as should probably be familiar with certain things They should be able to do using a raise and a rage reversal and a little bit of an introductory exercise to help you visualize this Process so what is an array? An array is an ordered data structure. The key here is ordered Arrays in Java hold one type of data. They can be primitive types or they could be reference type meaning objects and You know if you're coming from Python, for example, you know lists are kind of similar to a raise You can put an integer you can put You can put a string but you could mix you can put a raise You can do anything you want with a Java list or see a Python list, but job is a little bit stricter about that sort of thing Arrays are fixed in size. So once you've created the size of an array So this array has 10 elements. It always has 10 elements. You can't add elements You can't remove elements now you can set if it's a reference type You can set it to null, but there's still 10 places in memory reserved for that array But what is nice is it is randomly accessible through an index So you if you want to access element one or element 10 million it doesn't matter you can access it just as quickly It's one of the strengths of arrays So creating an array. So you'll see this pattern type Square brackets name equals new type and then the number of elements also in square brackets So for example int square brackets scores equals new int 7 so that this tells you this that there are seven elements In this case each element is an integer and the default value would be zero so As a reminder just like with strings the first element has an index of zero and the last element is index Six so it's zero one two three four five and six which gives us seven elements in total As I mentioned the default value for an array of integers is zero So if you try to print out this this the values you would see all zeros at this point Now we can also create arrays that are pre populated with values So for example string brackets weekdays equals We have Monday Tuesday Wednesday Thursday Friday. So you can see here. There are five elements The indexes are zero one two three and four So in this case, you know, we know up front what the values were going to be and we set now We could change as if we wanted to you know, let's say you want to change the language or whatever But this is a way of pre-populating if we know the values in advance So accessing and modifying array elements So to access and modify an array element, you need the index as I mentioned zero through It's gonna be length minus one. So you would if you're accessing you see say the value equals name Square brackets index if you're modifying its name index equals whatever that new value is that you want it to have Now I use value here, but it could also be an object I just chose value because typically with arrays a lot of times you'll see, you know Intoders and doubles and that sort of thing. But yeah, you also can use reference types Array traversal now, this is super-duper important. This is what I talked about earlier in the strings unit and with the loops Especially is this is that pattern I talk about with my students all the time it comes up in the free response questions It comes up basically everywhere So we've created an array and same thing seven elements So for int I equals zero, so I in this case represents the index That is the I didn't come from that it comes from an older programming language where I was an integer and So you might see you'll see IJK used a lot of times So we're starting at zero because indices start at zero While I is less than scores dot length now notice It's the array name dot length. There are no parentheses here. Okay with strings. It was length parentheses But with arrays, it's length. Wait, wait, wait to a rail list because they do their own thing and then we Increment I plus plus So here's that pattern. So here's our for loop. Then we pull out each individual score So the first score equals scores I Then we do something with that score Okay, so this is that standard pattern that I I've talked about before in these videos And I really emphasize with the students because they have a lot of trouble with this and keeping consistent is important Another thing I really emphasize with the students is this is an array So I asked them to make sure the name of their arrays are plural Because it's a group and then when we pull one item out, it's singular. So score scores name names A person you can do persons or people So student students it just keeps a certain level of consistency It helps especially for ESL students who I have quite a few because I teach it in international school This is this is a really helpful thing to keep that consistency We can do the exact same thing with a while loop. Although typically we use for loops You can use a while loop But I really just kind of go with for loops. I'm just more comfortable with them And I think they suit this this pattern a bit more Naturally, so same thing our Our counter here our index is I equals zero The condition is I is less than scores that length. No parentheses And in here, it's the same pattern We pull out one score at a time and we do something with it and then we increment you forget that you get stuck in infinite loop Go back a little bit. We can also do what's called an enhanced for loop I talked about this earlier in the one of the earlier units But this is the first case where you can really use it without Having to do something special, you know the previous case. We had to use the two car array Method so the four the enhanced for loop was pretty cool So what it does is it skips that step where you have indices and pulling out the individual item It just does it for you so for If you think about this it's sometimes called a for each loop So for each score in the arrays score Then we do something with it. So we print it now We would use this when we're definitely starting at the first element and going all the way to the end If we wanted to go from the end to the beginning, we couldn't do that We'd have to use a radar for loop or a while loop But those are different ways we can traverse your race with a for loop a while loop or for each loop or enhanced for loop as they're called in Java there are a number of standard algorithms. The students should be familiar with so these are things that might come up on the AP exam either in a mobile choice type question or in a free response question The students should be comfortable finding maximum and minimum values From an array. So how do we find that value? It should be comfortable computing sums and computing averages So this goes back to the loops basically the loop unit has a little bit of this as well Determining if at least one element meets a certain criteria So you have a group of students is is there at least one senior in that group? How could how would you determine that? Determining if all elements meet a certain criteria. So you have a group of students Are they all seniors or are they not all seniors? How do we do that? Also Counting this is a big one. We did this earlier with counting vowels in one of the earlier videos or at least in my book We do that Determine the number of elements that meet a criteria So, you know, how many students are seniors how many students are juniors how many students are sophomores, etc, etc Shifting array elements left or right. That's an interesting exercise And if the students can understand that and they have a really solid grasp for the most part on looping and iterating and all that sort of thing That requires use of a temporary value because otherwise you lose one of the values And then reversing the order of the elements definitely make sure the students know how to do that in place And also be careful with reference types because that can have some issues with that So and this does I have seen this on the AP exam at least AP Previous AP exams have had this so make sure to just know how to You know work through that and make sure that they can trace the code for that particular one Interaction exercise again arrays are not a hard concept especially after doing Strings is basically analogous to a string except instead of just each character of the string We have an object or value primitive So what I do is usually just write a set of boxes on the board and then just ask the students to for some values Just random numbers and then I give the students two slips of paper and I ask them write down one access So system out print LN and whatever, you know, those values are you know, it could be a RR AR 5 AR 3 and then one modifier AR 6 or you know equals You know, whatever value and then we take those just randomly pull them out And then right either right on the board or put them on to the screen somehow if you have a projector and then just work through it Okay, so what you know, how do we change the values of that array based on these particular? Commands and I'll throw a couple in there, too Where I'll do like array 3 equals array 4 just kind of see if they can get the idea Okay, well we can copy values from one box to another box. It's pretty cool So yeah, you'll find that that way they get an idea of how that system works and it's a little bit visual a bit more active Yeah, I like getting the students up and writing stuff on the board and that sort of thing So yeah, so we looked at what is an array how to create an array. There's two different ways And it created basically an array with default values or pre-populated array Saw how to access and modify the array elements Array traversal with for loops and while loops array traversal with enhanced for loops Some of the standard algorithms that students should be comfortable with and hopefully a little introductory exercise to get you started So good luck with that See you soon