Java Project-1
Java Project-1
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
Aim of Project:
Syntax:-
Vector Constructor:-
1. Vector():
Creates a default vector of the initial capacity is 10.
1. add():
The add is vector class method which is used to insert the specified element in
the given vector.
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():
3. capacity():
Returns the current capacity of this vector.
4. Size():
5. firstElement():
6. lastElement():
7. Get(int index):
9. Remove():
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:-
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:-
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