Welcome back.
And this video, we're going to look at ArrayList and we're going to use the rails quite a bit in our
future project, so it's important to see how they are used and understand them so they're used to create
dynamic arrays and arrays are types of collections.
So we're going to use collections here.
So why should you use an array lists?
Well, relics are used to create a dynamic array, which means the size of an array list can be increased
or decreased according to your requirements.
So it's not limited to a specific size, which is pretty cool.
The ArrayList class provides both read and write functionality.
Then the ArrayList follows the sequence of insertion order, so it matters when you put something in
there and then the radius is non synchronized and it may contain duplicate elements.
So it's not going to be limited to one entry of the same thing, so to speak.
The constructor of an array list looks like this.
So either this used to create an empty ArrayList, so that's the basic constructor.
Then you have another constructor where you can actually pass in the capacity so you can see what the
maximum amount of elements that can fit into this array test should be.
And then you can create and the rate is based on a collection so you can fill it with the elements of
a collection and use it then as you need it.
Examples of functions of a rapist are, for example, the add function used to add the specific element
into the collection.
Then the clear function is used to remove all elements from a collection.
To get function allows you to get an element as the specified index.
So, for example, if you want to have the fifth entry, you could enter index for in there and you
would get that.
And then the remove function is used to remove a single instance of the specific element from the current
collection if it's available.
And hereby I say a single instance, so let's say you have to value five multiple times in there.
Only the first five would be removed if you call this remove function with the value of five.
Then there are, of course, many more functions in the ArrayList class, and we're going to use multiple
of them, but these are the most important ones.
All right, let's have a look at an actual ArrayList so you can see we create an array list and we created
via the valid keyword.
So we're not using VAR here because even though the ArrayList might be a vowel, so an immutable ArrayList,
it's still adjustable.
So we cannot replace the ArrayList object, but we can replace the content of this ArrayList object.
In this case, it's in ArrayList object of type string, so we only can add strings in there.
For example, the ArrayList add one and then ArrayList add two.
So I'm adding one and two to this ArrayList, meaning the written words.
And then we can print this ArrayList by using the for in loop here.
So for I in ArrayList, we print I and that will output one hand to the rest using collections.
Let's have a look at that.
We can see we have again the function main and then we create an array list this time.
We set this list as being limited to five entries.
So this is the one example where you can limit an array list to add a certain amount of values.
So here this one, and then we have a list, which is a mutable list of strings and we put values in
there and then we add all of those values to our ArrayList.
Then we use an ArrayList iterator called Ittihad in our case, and we say as long as this iterator has
something so as long as our ArrayList has another entry in there, we run to run this while loop.
So we're just printing it our next.
So the next entry in our ETR, which in this case means in the ArrayList iterator, which pretty much
means in the ArrayList itself.
And then we can also see that we can get the size of an ArrayList by using its size property.
So that will give us the following output one two and then, of course, the size of a realist as to
because there are only two entries in there.
Now, let's look at the get function, so you can see we have this main function here again with.
So then let's look at the get function you can see again, we have a main function here using an array
list of strings and then we add one and two and then we loop through it with this for loop.
But we also used to get function here in order to see what we will get.
So if I use get one, I will get two.
So this is a little confusing, but that has to do with the counting starting at zero.
So the first entry here is going to be at index zero.
The second entry will be at index one.
And what we're doing here is we are accessing index one, which means we get the two, and that's why
the output is two in this case.
All right, this was just a little introduction to ArrayList.
We are going to use them quite a bit and then they will become natural as usual.
So no worries there.
It's like learning a new word in a foreign language.
The more context you see this word and the more it makes sense and the better of an understanding you
get for this word.
So see you in the next video.