Java Essentials - Vectors in java
11K views
Oct 24, 2024
Java Essentials - Vectors in java
View Video Transcript
0:00
In this video we are discussing vectors in Java
0:03
In the previous video we have discussed array list. And vectors and array list are almost same, but there are two major differences between
0:12
vectors and array lists. So let me go for further discussion. So what is vector
0:18
So vectors are also a dynamic array like our array list. That means in case of vector also, we can go on adding our elements to the vector as when
0:27
required but it has got two main differences so at first we're discussing
0:31
difference number one so vector is synchronized but at a list are not that
0:36
means that in case of vector so vector objects will be created and methods which
0:42
are there under this vector object can be called under this vector class can be
0:46
called but in case of multi-threated environment at a time only one
0:51
threat can call these methods and that's why it is called synchronized but in
0:56
case of array list in case of multi-threaded environment we can have multiple threads can access
1:02
the methods of this array list object so there is a basic difference between this vector and the
1:07
array list so that why it is our suggestion that for the single threaded environment we should use this array list for the multi environment we can use this vector objects but these array lists can in case of multi
1:22
environment we can use collections dot in case of addilists we can use collections dot synchronized
1:28
list function to create a synchronized list and thus getting an equivalent at a list which is
1:36
equivalent to a vector so what is the function name we can use a here that is a collections dot synchronized list function okay now let us go for the second
1:46
difference the vector contains many legacy methods that are not part of the collections
1:53
framework so there is a another difference between the vector and the array list the functions
1:59
of the vectors and the addilists are almost same so we have defined we have discussed so many
2:04
functions in the addie list so now let us go for one practical demonstration where we'll be using
2:10
some methods from this vector objects so here is the demonstration for you for the better
2:15
understanding here in this particular program we have defined one num vector which is the
2:22
object under the class vector and which will contain integer type objects so here you can find that it is containing integer type objects so integer is nothing but a wrapper class under which objects will be defined and that those objects will be kept under this
2:39
vector object numb vector so now we are going to add some items to this
2:45
particular vector object so numb vector dot add is a method with the help of
2:50
which you can add some items into this vector object numb vector
2:54
so here we have added say five such objects and these objects have got added with this
3:01
num vector so now if I want to print the size of this numb vector so that means the
3:06
number of objects it is containing we are expecting that it will print five here
3:11
so let me go for the execution here you can find that the size of the vector is
3:17
being shown as five because here we have added five objects of integer type so now
3:24
are going to print all the members of this numb vector so in that case I've just I've
3:30
just printed this one in this way the system dot out of print alien numbers with a
3:35
concatenation with this num vector that is a vector object so what is outcome in
3:39
that case we are finding that we finding this 10 20 30 40 and 50 they have got printed now also we can access this members using a 4 each loop just see here this
3:51
the integer class under this integer wrapper class we have defined one object that is a num and using for each loop we are reiterate we are through the
3:59
iterations we are retrieving each and every element one at a time through these
4:04
iterations and they are getting printed as a result of that we are getting
4:08
the outcome that is 10 20 30 40 50 got printed in the separate lines because here we have used print LN function here
4:16
so now we are going to add another another integer object there is a 100 to this at the
4:23
location number three so subscript number three we know that in case of vector the
4:28
subscript will be starting from the index will be starting from zero so as the index
4:33
will be starting from zero so at the third index we're going to insert this
4:37
hundred so now later we are going to print this updated num vector there is a vector object so we are finding that this hundred
4:46
has got inserted at the index three here because zero one two three at the third
4:52
index the hundred has got in way inserted so now if we go on printing the size
4:59
of this
#Java (Programming Language)
#Software