 Welcome to intro to Java with a focus on AP computer science a with Tokyo EdTech. That is me today's topic array lists So here's what we're gonna take a look at Well, what is an array list? We're looking at how to create them how to access the elements in an array list how to modify the elements in an array list How to traverse array lists using for a while and enhanced for loops and we'll look at developing algorithms again So if you saw the arrays units Hopefully you have this. This is the next next unit It's very very similar But the big difference is that Array lists when you create them they are empty and then you can add elements and you can actually remove elements Which is pretty cool. So they are not fixed in size. You can add stuff delete wherever you want So let's go ahead and take a look at the code So the first thing that you know about array lists is that they actually need to be imported So they are not part I guess guess you say of the actual core core Java language So we need to import and notice this import is going before the class name So just like we did the Java util scanner, we're gonna be importing Java dot util dot array List and it's singular not plural So now that we've imported that we can use that so what we're gonna do is we're gonna create and array of Computer languages, so watch this weird syntax. So array list less than string Languages again notice I always keep it plural and something's wrong with the spelling there languages equals new not you new array list string and parentheses Isn't that weird? I think it's weird. So I'm gonna compile that and it compiles successfully so What we want to do here, I wish you could go after I guess I have to modify first Let's go ahead and just put this in here then so now I have an empty array list Let me go ahead and add values to the List so I want to say languages dot add Go with Java Our main language here Go ahead and add a few of these so Java we've got Python We've got uh jump script Well, let's go keep we'll keep with our old school languages and we've got c plus plus So if again, I'm gonna compile this Now if I you know run it nothing's gonna be happy because we didn't print anything. So let's go ahead and Uh put some stuff So if you think about this, this is just like an array So we're going to be using indices So this would be index zero. This would be index one. This would be index two This would be index three because of the order that we added them because we're adding them at the end So if I do system dot System dot out dot print l n and it's gonna be languages Now if it's an array we would use square brackets But we don't So we're gonna use methods public methods. That should be familiar to you now. We're gonna use get so get zero For example, I'm gonna go ahead and two three Hey So zero One two and three, let's go ahead and compile that And you can see we got java zero python one javascript two and c plus plus three We can also modify those just like we did with arrays. So let's say Yeah, we don't like c plus plus. So we'll go to we'll say languages So again instead of square brackets, we're going to use dot set We need the index in this case three And we're going to change c plus plus to C sharp which is a bit more Uh, more java friendly and then we'll go ahead and print that and see if we actually were successful in doing so So let's go ahead and compile it Okay, so we went ahead and we changed c sharp or c plus plus to c sharp Pretty cool. Okay. So notice We've created An array list. Okay. So we have string here and string here now There are some cases in the ap where you'll see those not matching You know Pay attention to the ap materials for that And then we had to add items to the list to the array list We accessed using get And we changed it Using set It's about that it's pretty straightforward. So traversing an array list is very similar to traversing a regular array So for example We would just do four int i equals zero i is less than now watch this one languages dot size And then i plus plus Okay, so with strings it's Dot length with parentheses with arrays. It's dot length and with array list it is Dot size parentheses. So yeah, lovely, right So then what I would do here is I would say string language equals languages dot get i then you know system Dot out print l and print l and language So again, I'm hoping you see is this pattern. It doesn't you know From this part here in this part here nothing really changes just the way that we access it You know between an array and an array list or a string whatever that that collection is And then the way we get the Size or the length the number of elements differs depending on the structure that we're using So if I compile that and run it we should see Java Python JavaScript C sharp. I can do the same thing with a while loop int i zero While i is less than languages dot size So I pull in there you'll get a weird result. And then at this point I could just copy and if we run that Compile and run it oops. Oh, what did I forget do? We'll see my bad I plus plus I forgot to do that so compile and run And you can see we've got Python JavaScript printed out again. And then finally we can do the same thing with enhanced for loop. So for a string language in languages and system Out print l in language So a lot of it's very similar There are a few different Changes there and we see we got the same thing three times in a row. All right So uh developing algorithms, so let's go ahead and Basically kind of redo the maximum value one that we did in the last one So there should be some similarities or should be some differences. So to make it an array list And in this case, we're going to use integer and we're going to do scores equals new array list Integer or the weird syntax Note here I Use the integer what's called the integer wrapper class What this is this is an object that stores an integer So for a rayless we need to use You know objects the other thing you need to think about or you need to be aware of is that Even though it's an integer. There is like a get value method. There's there's a couple different methods that go with it Java will automatically convert it for you. You don't really have to think about it But in this case, we want to use the integer wrapper class And that's w r a p p e r not wrapper r a p p e r Let's go ahead and add some values. So scores dot add And this is I think basically the same thing that we did in the last video Arrays two three four five six. Yes, it is So I think the values were 45 So they should look familiar if you saw the last video recently 75 97 23 and 76 so Just like we did before we're going to assume the max value Is scores dot get zero So this could be the Excellent value. We don't know We do because we've seen all the scores, but we can it's possible And then we do our for loop now. I'm going to go ahead and Comment this out. So I don't have to deal with that little weird problem again You don't want me to do this We've got somewhere I forgot about and yeah, that's good. So for int i equals One because you know, we don't need to check this. We know We're going to start with one. We talked about that in the last video as well Uh, I is less than scores dot size I plus plus And then again, we're going to do int score equals scores dot get I And then from here on it's the same thing as we did before. So if max Value is greater than this or sorry the score Is greater than max value Then we set the max value to the score because that's the new maximum and we do system out print ln max value plus max value I could have called a max score and that probably would be a little more sense. That's okay And we can see the max value again is 97 So the the basic algorithm the basic steps do not change The way that we you know create the structure create the collection The way that we add values the way that we get values is a little different But the actual algorithm itself doesn't change. There's there. This is how you do this Because we have to traverse and go through all of the values in that That's you know in that structure in that collection Okay, I think that's enough for this video that that should get you started Again, there's some more information about developing algorithms in the ebook And there's a few things the the ap you know talks about beyond this So be sure to pay close attention to your ap materials Yeah Test is coming. So again, we talked about what an array list is how to create it Don't forget to import java util array list. That's very important We use we talked about how to access using set or sorry using get how to modify using set and there's a few other You know array list methods that you can use We learned size for example Check out the ebook for a few more details And then we used for a while and enhanced for loops to iterate through and develop a little bit of an algorithm to find a maximum value So hopefully that'll get you started. Thanks for watching