You are on page 1of 10

 Introduction:-

 Vector implements a dynamic array which means it can grow or shrink as


required. Like an array, it contains components that can be accessed using an
integer index.

 They are very similar to Array List, but Vector is synchronized and has some
legacy methods that the collection framework does not contain

 It also maintains an insertion order like an Array List. Still, it is rarely used in
a non-thread environment as it is synchronized, and due to this, it gives a poor
performance in adding, searching, deleting, and updating its elements

 This vector is found in java.util package and implements list interface

 The vector class implements a growable array of object.

 Aim of Project:

 The aim of the project is to understand evolution in mobile generation in the


history of mobile networking.
 Course Outcomes Achieved:
• We can understood Vector Methods.
• Learnt types of Vector.

• Learnt Vector Constructor.

 Syntax:-

 Vector <data type> object name=new vector <data type> ();

 Vector Constructor:-

1. Vector():
Creates a default vector of the initial capacity is 10.

2. Vector (int size):


Creates a vector whose initial capacity is specified by size.

3. Vector(int size, int incr):


Creates a vector whose initial capacity is specified by size and increment
is specified by incr. It specifies the number of elements to allocate each
time a vector is resized upward.
4. Vector(Collection c):
Creates a vector that contains the elements of collection c.

 Methods of vector class:-

1. add():

The add is vector class method which is used to insert the specified element in
the given vector.

 add ( Int index, E element):

This method is used to insert the specified element at the specified position in
the given vector.

 add(E,e):

This method appends the specified element to the end of this vector.

2. add element():

It is used to append the specified component to the end of this vector. It


increases vector size by one.

3. capacity():
Returns the current capacity of this vector.

4. Size():

Returns the number of components in this vector.

5. firstElement():

First is used to get the first component in this vector.

6. lastElement():

It is used to get the last component of the vector.

7. Get(int index):

It is used to return elements of specified position in the vector.

8. Element At(int index):

It is used to get the component at the specified index.

9. Remove():

It is used to remove the specified element from the vector.

10.Remove all():

It is used to delete all the elements from the vector that are present in specified
collection
11.Remove element():

It is used to remove the first (lowest index) accurance of arguments from the
vector.
 Program 1:-

1. import java.util.*;
2. public class VectorExample {
3. public static void main(String args[]) {
4. Vector<String> vec = new Vector<String>();
5. vec.add("Tiger");
6. vec.add("Lion");
7. vec.add("Dog");
8. vec.add("Elephant");
9. vec.addElement("Rat");
10. vec.addElement("Cat");
11. vec.addElement("Deer");
12.
13. System.out.println("Elements are: "+vec);
14. }
15. }

 Output:-

 Elements are: [Tiger, Lion, Dog, Elephant, Rat, Cat, Deer]


 Program 2:-

1. import java.util.*;
2. public class VectorExample1 {
3. public static void main(String args[]) {
4. Vector<String> vec = new Vector<String>(4);
5. vec.add("Tiger");
6. vec.add("Lion");
7. vec.add("Dog");
8. vec.add("Elephant");
9. System.out.println("Size is: "+vec.size());
10. System.out.println("Default capacity is: "+vec.capacity());

11.
12. System.out.println("Vector element is: "+vec);
13. vec.addElement("Rat");
14. vec.addElement("Cat");
15. vec.addElement("Deer");
16. System.out.println("Size after addition: "+vec.size());
17. System.out.println("Capacity after addition is: "+vec.ca
pacity());
18. System.out.println("Elements are: "+vec);
19.
20. if(vec.contains("Tiger"))
21. {
22. System.out.println("Tiger is present at the index " +
vec.indexOf("Tiger"));
23. }
24. else
25. {
26. System.out.println("Tiger is not present in the list.");

27. }
28. //Get the first element
29. System.out.println("The first animal of the vector is = "
+vec.firstElement());
30. //Get the last element
31. System.out.println("The last animal of the vector is = "
+vec.lastElement());
32. }
33. }
 Output:-

 Size is: 4
 Default capacity is: 4
 Vector element is: [Tiger, Lion, Dog, Elephant]
 Size after addition: 7
 Capacity after addition is: 8
 Elements are: [Tiger, Lion, Dog, Elephant, Rat, Cat, Deer]
 Tiger is present at the index 0
 The first animal of the vector is = Tiger
 The last animal of the vector is = Deer

 Advantages:-

1. The dynamic size of vector avoids memory wastage

2. The size of data structure can be changed any time

3. Vector is more advantageous as compare to array

4. Vectors are synchronized


 Dis-advantages:-

1. Vectors may have more memory overhead compared to arrays due to


additional information (size, capacity, etc.)

2. Dynamic resizing and memory management might introduce slight performance


overhead compared to the direct memory access of arrays

3. While Vector is still supported, newer Java code is often written using the more
modern collection classes, so it may be harder to find examples and support for
Vector

You might also like