 to AP Computer Science in Java Unit 7. Keep in mind for your information, this video is being recorded one year-ish after the last video was done. I'm sorry, somehow I forgot to do Unit 7. So let's get to it. Unit 7 is something called ArrayLists. So hopefully you've just come from Unit 6 so you know what an Array is. An ArrayList is very, very similar, but it has a few different features that make them very, very useful. Probably the biggest useful feature is that you don't have to define the size of an ArrayList. You can just keep adding to the ArrayList as you wish. Also, ArrayLists are accessed with methods as opposed to indexes with square brackets, for example. So let's take a look and get started doing this. Now, ArrayLists are not part of the core built in Java. So what that means is we actually have to go up here and import java.util.ArrayList. Okay, if you don't do that, you're gonna get some kind of weird error. You see that double dot there? My dot key, my period key is a little bit messed up so you'll see that a lot today. So just like with everything else, we need to declare it. Now, the ArrayList has this very odd syntax and I'm gonna go ahead and show you what it looks like. So we type ArrayList. So it's very much like declaring any type of object that we might have done. So you do the name. Now, this is important. Notice that is a less than symbol. And let's say we want an ArrayList of strings. Okay, so we have to declare the type of the ArrayList. And I'm gonna make an ArrayList called Superheroes, okay? And it's gonna be a new ArrayList and again, String. And don't forget, it has a very odd syntax, the parentheses here and the semicolon. Now I'm just gonna go ahead and compile that and see if it compiles and I didn't get any errors. So probably this is gonna work. So ArrayLists, as I said, are a little bit different to Arrays. So if we want to find the, how many items are on the ArrayList? Now right now it should be zero. We're gonna use something called dot size. Print ln and our ArrayList is named Superheroes and it's dot size, okay? So let's go ahead and run that, compile and run it. And you should see a zero here because we haven't put anything onto the, or into or onto the ArrayList. So let's go ahead and do that now. And let's go ahead and add some Superheroes. So I'm gonna go Superheroes.add and notice it is add. And we'll go ahead and add Clark Kent. We'll go ahead and add, let's see, add Bruce Wayne and heroes.ddadd. And we'll go ahead and we'll add Diana Prince. And so yeah, we'll just stop there. So now if I printed the size of this ArrayList I should see three because there are three items on the ArrayList. Let's go ahead and test it. You notice I do like the test. That way I know my code is working as expected. So now we've got three items. Now just like with any other, with any other array, they have an index. So ArrayLists are ordered just like arrays. So this is gonna be the zero with index. This is gonna be index one and this will be index two. So if I wanted to print out Diana Prince I would do the following. So system.out.println and I'm gonna do Superheroes.get and the index. Okay, in this case it is two. Let's go ahead and try that. And you can see we've got Diana Prince and that is one of Wonder Woman's aliases. So yeah, that's cool. So now this is how we pull information out of the ArrayList. So let's go ahead and put some information. We've already put information into it. Let's go ahead and change some of the information. So watch what I do here. So Superheroes.set. So I wanna set index two, which is Diana Prince. I wanna change that to Wonder Woman. And then if I go ahead and print that out we'll see that it changed. Okay, so Diana Prince has been changed to Wonder Woman. Now what's really cool about this set is that, you know, you don't see it here it actually returns the original value. Okay, so let's go ahead and do the same thing with Bruce Wayne. So I'm gonna say String, let's see, how would you say it? I don't know how would you say it? So Identity equals, in this particular case, Superheroes.set and Bruce Wayne is index one and Bruce Wayne of course is the Batman. And so I'm gonna do System.out.println. We'll say Superheroes.get one. Plus is, plus, quote, or plus Identity. Let's put the period at the end to be consistent. Okay, so what I'm doing is I'm setting Bruce Wayne to Batman. And then what happens here, it actually returns the original value. So that way we don't lose that value in a case where we need it like this particular once. Let's go ahead and run it. We'll compile and run it. And you'll see the Batman is Bruce Wayne. So Identity is now Bruce Wayne. But Superheroes no longer has that information because we've changed it to the Batman. Okay, so if I go ahead and print it out, Superheroes at this point, this is what we're going to get. And you can see that these are in order. So we've got Clark Kent, the Batman and Wonder Woman. Now it is possible to remove items from our ArrayList. So to do that, I'm just gonna go ahead and do Superheroes.remove. So let's say we wanted to remove Clark Kent. So I'll remove zero. And then if I go ahead and print Superheroes, you'll see that Clark Kent is no longer on the list. So we can remove items from an ArrayList as well. So those are kind of the basic ways that we can interact with an ArrayList. So first we created it. We add items to the ArrayList. We can get the size. Now if this was an Array, we would use dot length instead of dot size. So you have to keep that in mind. And then we can print out individual items by using the get method. We can use the set method to change a particular index to a different value. And then we can also return that value using the set method. So before we change it, we get the original value. So that can come in handy. And then we can also remove values from our ArrayList. Pretty cool stuff. So let's go ahead and take a look at Traversing ArrayList. Now Traversing ArrayList is essentially the same as Traversing ArrayList Array. I guess that should be no space there. So let's go ahead and do for int i equals zero. Again, we're starting at zero. i is gonna be less than Superheroes dot size. And it's a method. Don't forget the parentheses. And we're gonna increment by one. So then what we'll do is we'll do system.out.println. Actually, let's do it this way. Let's go ahead and do string superhero. And we'll just leave it like that. And this is something that's kind of a new way I'm teaching this. I think it helps beginners especially. Superhero equals superheroes dot get i. And I'm gonna print Superhero. And I'm gonna close off that loop. And let's go ahead and try it. Okay, so now we only have two items here. So the Batman and Wonder Woman. And you can see here it's the same as arrays. It's the same as Traversing a string. And we just start at the zeroth index. We go up into the limit. Notice that's less than, not less than or equal to. That's always plus one. And then we increment by one. Then we pull out the information we need. So superheroes is an array list made up of individual, an individual superhero at a time. So we pull each superhero out one at a time. And we print it. Okay, now we can also do the same thing with an enhanced for loop for string. Now we'll probably get an error here, which is fine. We'll fix that in a second. Superheroes. And then this is a little bit easier. We just do system.out.println.superhero. I'm gonna close that off. Again, I'm predicting we'll get an error here. And yeah. So it says variable superhero is already defined. Okay, so because I did this up here, I don't need to put a string here. We already know that superhero is a string. So I can fix that. Or actually, I can't fix that, sorry. So you can see here, we got a little bit of an error. So probably what I would do in this case is I would do string S. You'll see that's kind of a common pattern. And then I can, now I can go ahead and do that. Okay, again, I don't particularly like that. I like to put the full thing in. But in this bigger case, I didn't really have a choice. So that is how we traverse array loop, array lists, using either a regular loop or an enhanced for loop. Now, for the next part, developing algorithms, and this is exactly the same, oops, I shouldn't have put that in the wrong spot. Yeah, the scrolling on this program is terrible, on a Mac at least. And so let's go ahead and put that there where it belongs. Go ahead down here. I do like to have things in a pretty consistent order here. All right, algorithms. So, one of the algorithms we're looking for here is calculating, let's go ahead and calculate an average. Or actually, no, we're gonna go ahead and find the maximum, okay? So let's go ahead and create an array list. So we're gonna create an array list and I'm gonna use integer. Now notice I didn't use int, okay? So my belief is that with array lists, it must be an object, okay? So we use the integer wrapper around an integer, okay? So new array list, yeah, I think you should know what that is by now. Hopefully if not, put that one up I guess. So I've created an array list of integers and then I'm gonna go ahead and do averages.add. I'm just gonna go ahead here and add a few averages. So I think of these as test scores, for example, or average scores for a student at the end of the semester. These were not, these are just, I think random numbers I picked, maybe seven, 45, 75, 99, 93 and 78, okay? So I've added six averages to the array list. Now what I wanna do is I wanna find the highest number. So I'm gonna go ahead and do int max equals averages.get0. Now I don't know that 87, zero, the zero index is the highest number, but I know it could be the highest number. So watch what I do here. I'm gonna do for int i equals one. So what I'm gonna do is I'm gonna assume this is the highest number, but I'm gonna compare it against all of these other numbers just to make sure. So I'm gonna say averages, so i is less than averages. Remember it's size, parentheses and i plus plus. Okay, so then what I can do is if, well actually let's go ahead and do int average. So what I'll do here is I'll say if, so I'll say average equals averages.get i. So that will pull out each individual average one at a time. And then all I can do is just check it. So if this current average is greater than the max, the max now equals that new average. So I need to go through every single one of these. And then at the end, I'm just gonna go ahead and print out the result. So I'll say the max is plus max. So just to kind of explain that again if you didn't see the other videos. So we take 87 and then we compare it to 45. Is 45 greater than 87? Nope. Is 75 greater than 87? Nope. Is 99 greater than 87? Yes it is. So in this case, I would then change the max to the new average, which is 99. And then of course I would continue going and we got, oh that's so annoying, sorry. Is 93 greater than 99? Nope. Is 78 greater than 98? Nope. Then we just print out the max that we found. And the same thing would work with the minimum if we wanted to reverse the greater than the less than. So the max is 99. So this is a very common algorithm type thing where we're searching through an entire array or in this case, a ray list and we're looking for a certain thing. In this case, we're looking for the maximum, okay? And then you can probably see that the same pattern would be used for finding something in an array list, okay? Now I could have also done four int average in averages. I could have done an enhanced for loop here, but yeah, I'll just do it that way. So searching works exactly the same. So I'm gonna go back to my superheroes and I'm gonna say for int i equals zero, i is less than superheroes dot size, i plus plus. And let's say I'm searching to see if, let's say the Batman is a superhero. So I might say, so superhero equals superheroes dot get. Again, we're using the get method. And again, I'm doing this just because it's a lot clearer for beginners. I need to first pull out the information I need, then I'm gonna go ahead and compare it. So if superhero, hero, hero dot. Now remember, this is a string, so I gotta use equals, not the equal sign. Dot equals, and we'll say the Batman. So if the Batman's on the list, then I would say system.out.println, the Batman is a superhero. But notice I still have to search through the entire list. Now in this case, there's only two items, so it shouldn't take long. Okay, I missed the parenthesis. Hopefully some of you caught that. There we go, the Batman is a superhero. And so let's go ahead and just change that to see if, make sure it's working. So I'll say superman, I forgot, but I didn't put Superman on the list. Okay, and it doesn't show up. So that's what we expected. Now again, I could also do this with an enhanced for loop. So for string, string s, I'm not sure if this is gonna work, superheroes. Okay, so if s.equals, go ahead and copy this, because I'm kind of lazy, close that off. Let's see if that works, or if we get that error. An error. Okay, now this one, it let us do that. So, you know, it matches the original one. So you see how it basically functions the same way as the other one. And because I didn't put a period there, I didn't get a period, but that's okay. Okay, so I think that's all I'm gonna do in this particular video. I'm kind of running out of time here. But sorting, watch the unit six arrays video. And I think in there I talk about sorting and how a selection sort works, how an insertion sort. I also have two like independent videos, one about selection sort, one about insertion sort, that explains these in details, or in detail. But the only thing you have to keep in mind is that since it's an array list, you know, you're gonna be using get and sets and all that sort of thing, instead of using just the indexes and brackets. Okay, so real quick, don't forget to import java.util.arraylist. And again, we have this weird syntax for setting up, creating an array list. And don't forget to declare the type. And again, if it's an integer or an inter-double, use the wrapper classes, don't use the primitives. And then we can get the, how many items are on the array list by using dot size and parentheses. We can get a value using the get method. We can change a value by using the set method. And we can remove a value by using the remove method. Traversing array list is the same as traversing arrays. You just use the index and work your way through it. Again, you see here how I did this little kind of helper variable. I could have put superheroes.geti right here. I didn't have to do this step, but especially for those of you who are not, or have a little trouble, what information am I looking at? What information am I pulling out? This might help you to do that a little bit better. We can also use enhanced for loops, as we discussed earlier. And basically, some of the algorithms that you'll see in AP Computer Science, finding a maximum value, finding a minimum value, et cetera, et cetera. This is how you do it. You assume the first value is the correct one, and then you confirm it by checking against all of the other values. That's why this is not a zero, because we don't want to compare the first value to itself. We could, it's not a big deal, but it's just inefficient. And then searching works exactly the same way. We go through one by one, look for our condition, in this case, the Batman. And if it's true, then we do whatever it is we need to do. And again, sorting and with selection sort, insertion sort, check out my other videos for that. It should be pretty straightforward at this point. So good luck with your test, and yeah, hopefully you're almost at the end of the course, so take care. And as I'd say, keep on coding.