All right.
So in this video I'm going to show you a legacy list type called Array List.
And it is not recommended to use it anymore for new code or new projects that you're working on.
But when you're working with older projects, you might very well see array lists, and it's good for
you to at least understand how they work.
You don't need to use them yourself, and it's really just for you to know that they exist and how they
roughly work, so that whenever you see a legacy project, that you are not confused as to what that
is.
Okay, so let's look at this video.
It's a video that I recorded a few years ago.
It's a video that I recorded a few years ago, but it's still valid in terms of how it works.
Okay, so let's get started and let's go ahead and start by creating an array list.
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.collections using System.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 just should contain 100 objects.
This one here has an indefinite amount of objects with 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's 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 ints 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 dot 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 list.
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 at method.
And you can see there is a remove range.
So you can just simply say okay from that point I want to delete two values.
So let's say from point four.
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 at 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 obj 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 sum.
Maybe sum 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't simply add an integer to a double, I need to convert it.
So I'm going to use the convert dot two double.
And here I'm going to say obj because as object is an object or obj 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 obj is a double.
So if that's the case then simply use sum plus equal.
And here you can't just say obj.
As you can see I cannot 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 now finally else.
And here I could also of course check if obj 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 actually I called it sum.
And finally console dot read 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 array list.
So it says here count and count is five.
If we check out here how many values we had.
Well we had 71234567.
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 try to remove something.
But there 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 array list.
And as you can see there is plenty to know about array lists.
And they're pretty cool.
And in the next video we're going to go ahead and use even more methods of the array list 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 array list here instead of, for example, array list 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 object here, because any different data type that will be within my ArrayList
will work with this.
Because if we look at our ArrayList, we have an integer.
Here we have a string here.
Then we have a double in there.
So we have three different data types in there.
And we cannot just say okay object or this obj 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, okay.
In case it's not 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 and A and I will add some additional instructions or descriptions to the lectures.
All right.
So see you in the next video.