‫Welcome back.
‫In this video, we are going to cover a red list and at least some parts of it.
‫And let's go ahead and start by creating an ArrayList.
‫So I'm just going to create a new array list here, declare it.
‫So declaring an array list.
‫And as you can see, it's complaining because it doesn't know array list.
‫And we need to use system dot collections using system dot collections because the array list class
‫is within the collections class, as you can see, implements the list interface and so forth.
‫So now I can go ahead and give it a name and I'm just going to use it or call it my array list is a
‫new array list.
‫So that's one way of declaring an array list and another way is to already say how many objects should
‫be in there.
‫So in this case I'm going to say, okay, my array list should contain 100 objects.
‫This one here has an indefinite amount of objects with an indefinite defined amount of objects, and
‫that one is with a defined amount of objects.
‫With a defined.
‫All right, so now let's go ahead and add some objects or elements to the array list.
‫So my array list, dot add.
‫As you can see, my array list has multiple different methods, properties and extension methods.
‫So you can check some of those out and you can even see something like Clear will simply clear the whole
‫array list.
‫Then you have contains.
‫It will check whether there is something in there that you're searching for and so forth.
‫So plenty of interesting methods and we're going to cover some of them.
‫So I'm going to add something.
‫And as you can see, I can add an object.
‫And that's the beauty about array lists.
‫They don't need to contain strings only or only or anything else only they can contain objects.
‫So anything that is of any type of object can be used here.
‫So that means we can store integers in there, let's say the value of 25, then we can go ahead and
‫store strings in there, something like Hello, or we can go ahead and store doubles in there.
‫So 13.37 and so forth.
‫So I'm just going to add some additional values in here, like 13 my array list that ADD and 128 and
‫just another value to be sure will be 25.3.
‫And that's not a comma.
‫That's a point.
‫All right.
‫So now what I can do is I can use some methods onto my array lists.
‫For example, I can use the remove method.
‫And in here I need to define what I want to remove.
‫So I'm not just removing an index, I'm saying which object I want to remove.
‫So let's say I want to get rid of the 13, so I'm just going to use remove 13.
‫This will delete element with specific entry from the array list or with specific value.
‫Maybe that's better.
‫So it's going to remove 13 from there.
‫So let's say I have two times 13 in there, which I'm going to do like that, then it will remove 13
‫from my list.
‫Now the next one is to remove at a specific index, so delete element at specific position or index.
‫And that is done by using the remove ET method.
‫And you can see there is a remove range.
‫So you can simply say, okay, from that point I want to delete two values.
‫So let's say from 0.4.
‫So this one here, I want to delete two values.
‫So it's going to delete 128 and 25.3.
‫But I'm just going to use remove ADD and I'm going to remove the one at position zero.
‫So it should delete or remove this one here.
‫So the 25 and now there is something called count.
‫So if you want to know how many objects are inside of your array list, you can use the count keyword.
‫Now what I want to do is write that onto the console, so I'm just going to use it like that.
‫So write console count.
‫All right, now let's get to something very interesting.
‫Let's create a for each loop which will check all the objects in our array list.
‫And I'm going to call it object or object in my array list.
‫And it's going to go through every single object into inside of my array list, and it's going to be
‫object in that iteration.
‫So what we can do is we can check if object is of type int, then we can do some coding.
‫So let's say I want to add up all the numbers and of course I don't want to add the string in here,
‫so I'm going to create a new double result.
‫Or you could call it some, maybe some is more precise.
‫This will be the sum.
‫And now my sum should be the integer and the double that are inside of my array list.
‫But now as I can simply add an integer to a double, I need to convert it.
‫So I'm going to use to convert that to double.
‫And here I'm going to say object because as object is an object or object is an object.
‫It's not a integer.
‫It contains an integer, but it's of type object.
‫So I can cast it into a double and simply add it to a double.
‫But if it's not a double, I need to convert it into a double.
‫So if it's an integer, I can convert it to a double.
‫So I'm checking.
‫Is it an integer?
‫Yeah, it is.
‫All right, so let's convert it into a double and let's add it to our sum.
‫And here else, if J is a double.
‫So if that's the case, then simply use some plus equal.
‫And here you can't just say OBJ.
‫As you can see, I can simply add the object that has to do with this being an object and not a double.
‫And you cannot simply add an object to a double, as I just said.
‫So what you need to do here is to use casting.
‫So in here I'm going to say I want to cast that object into a double.
‫And if that's done, then please add that to the sum and I'll finally else.
‫And here could also of course check if J is string.
‫So if that's the case, then simply write it onto the console.
‫So just write.
‫OBJ All right, now let's write the console or use the console to see what the result was.
‫Result and not with a capital and I actually call the sum and finally console the right key.
‫Now let's run it.
‫And as you can see, we get five.
‫We get hello and 179.67.
‫So what are these numbers?
‫This five is the amount of elements inside of my ArrayList.
‫So it says here, count and count is five.
‫If we check out here how many values we had while we had seven, one, two, three, four, five, six,
‫seven.
‫And we removed the 13 and we removed the zero.
‫So now let's check out if the 13 is still in there.
‫So if we take that 13.37 and we add 128 and 25.3 plus the 13, as you can see, it gives us this result.
‫So deleting remove 13, it deleted the element with a specific value.
‫So deleted one element and not all of them as you can see here.
‫So it's removing a 13 and it just removing the first one.
‫So let's copy that and run it again.
‫And now it will remove 13 twice.
‫And as you can see, we get this value of 166.
‫So there was still a 13 in there.
‫So let's run it again when there is no 13.
‫And.
‫As you can see, all is good.
‫So it tried to remove something, but that was nothing.
‫So if there is nothing, as you can see, it just doesn't do anything.
‫Even though we tried three times to delete this 13 or remove the 13 and there was no 13 in there anymore.
‫All right.
‫So that was a little start into ArrayList.
‫And as you can see, there is plenty to know about ArrayList and they're pretty cool.
‫And the next video, we're going to go ahead and use even more methods of the ArrayList class.
‫Before we do that, however, I would like to quickly talk about something that I haven't talked about.
‫And this is this object here.
‫So why am I using object in my ArrayList here instead of, for example, ArrayList as the data type
‫or even integer or string?
‫Well, I'm using object here because object is the highest level.
‫Class that there is every other class is in a certain way inheriting from that class object.
‫That's why we can use objects here, because any different data types that will be within my array list
‫will work with this.
‫Because if we look at our array list, we have an integer here, we have the string here, then we have
‫a double in there.
‫So with three different data types in there and we cannot just say, okay, object or this object is
‫going to be an integer or it's going to be a string or a double, because that wouldn't be correct,
‫because it's all of those things, right?
‫And object takes just care of that.
‫So if we use this approach, then it's going to be smart enough to understand OC in case it's going
‫to be an integer, it's going to take care of that and it's going to add to the sum.
‫Otherwise it's going to add to the sum if it's a double.
‫But if it's a string, it's just going to print out the string.
‫All right.
‫I just wanted to make sure that this is totally clear.
‫That's why I added this little piece to the video.
‫If you have any other questions where you feel like it was not explained well enough, then please let
‫me know in the Q&A and I will add some additional instructions or.
‫Yeah, descriptions to the lectures.
‫All right.
‫So see you in the next video.