Java Essentials - Array list in java
16K views
Oct 24, 2024
Java Essentials - Array list in java
View Video Transcript
0:00
In this video we are discussing array list in Java
0:05
Using this array list concept, we can have the dynamic size of an array
0:10
So let us go for more detailing on this array list. So what is an array list
0:17
So the array list class extends abstract list and implements the list interface
0:23
And array list supports the dynamic arrays that can grow as when needed
0:29
actually it is extending the abstract list class and also implementing the list
0:35
interface so there are some methods associated with this add list so there are so many
0:41
methods are there here we are going to discuss eight of them so they are the very
0:46
common methods we regularly use in our Java code so first one is the add method
0:52
so it is used to add an element to the add list next one is the add all so add
0:58
multiple data or collections directly in the Adel list in one shot because in this
1:03
case we can add only one element at a time but in this case the multiple data
1:07
or collections can be directly added to this Adelist using the method add all
1:12
next one is the size which will give us the number of elements currently existing in the add a list next one is the contents so that is the checks whether the given data is in the ad or not
1:26
So in this particular slide, we have defined four methods. First one is the add, which will add an element to the ad-a-list
1:33
Next one is the add-all, which will add multiple data or collections directly to the ad-a-list
1:39
Next one is the size, the number of elements in the ad-a-list. And next one is the contents which checks whether a given-a-list
1:45
data is present in the array list or not. So let me go for another four. So next one is the
1:52
index of, returns the index of this list of the first occurrence of a specified item given
1:59
or element given. Next one is the remove, delete specific element from the array list which
2:05
is existing. Next one is the clear. So removes all of the elements from the array list and it will
2:11
clear it next one is to add a returns an array containing all the elements of this list in a
2:18
correct order and the runtime and the runtime type of the returned array is that of the specified
2:25
array so from this add a list we can take all the contents to one array and for that purpose we can
2:31
use this method that is two array so let us go for one practical demonstration on this add a list and there will be using these functions in our java code so that you can get the idea that how this add list can be used in our java program in this demonstration we are going
2:49
to discuss addilist here we have defined one addilist class object that is our city list which
2:55
will be containing string type objects so how to add the string type strings with this particular
3:03
city list so city list dot at Delhi city list dot at Kolkata in this way we are
3:09
passing multiple string class objects and they are getting added with this city
3:14
list that is the add a list class object now what is the size of this city list
3:19
so how many elements it is containing so that will decide the size so here we're
3:24
expecting the size should be five here so let me execute my code I'm finding that
3:29
the size of the array list is five here so that is a city list dot
3:33
size is actually giving us the count of respective string objects residing in this
3:39
city list array list class object so system dot out dot print ellen cities colon
3:45
concatenation city list you can find that the respective city names are getting
3:49
printed so Delhi Kolkata Hyderabad Chennai and Lucknow they have got printed
3:56
next using for each loop using for each loop we are going to retrieve city name one after another for each and every iteration one at a time So display the content of the add list one by one
4:09
So here we're using the for each loop so for string city colon city list system dot out dot print LN city
4:17
So it will produce outcome like this so here we're having this all this city names which are the string objects are getting picked up
4:25
one at a time in each and a iteration of this for each loop insert another city at
4:31
the index number two in the city list so in that case we can go for city list dot add
4:36
two comma Mumbai so at the second place we know that the first place has
4:41
started from the index zero so at the second place at the index number two rather
4:46
we are going to insert this Mumbai here so let us go for the outcome you can
4:50
find that zero one two at the second index we have insert
4:55
this a new city name that is a Mumbai you can find that other city names Hyderabad
5:00
Chennai Laknu which were at the second subscript third subscript and the four
5:05
subscript so they have got shifted so now if we go on printing the size of this
5:10
add-a-list object we're going to get here six in this case so in this demonstration
5:15
we have we have discussed how to deal with the add-a-list class objects in our
5:20
Java code thanks for watching this video
#Java (Programming Language)
#Programming
#Software