[Go to site: main page, start]

0% found this document useful (0 votes)
4 views7 pages

Java Vector

The Java Vector class is a part of the Java Collection framework that allows dynamic array-like storage of elements, with no fixed size limit. It provides various methods for element manipulation and is thread-safe, making it suitable for concurrent programming, though ArrayList is preferred for non-thread-safe scenarios. The class includes multiple constructors and methods for adding, removing, and accessing elements, as well as checking size and capacity.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

Java Vector

The Java Vector class is a part of the Java Collection framework that allows dynamic array-like storage of elements, with no fixed size limit. It provides various methods for element manipulation and is thread-safe, making it suitable for concurrent programming, though ArrayList is preferred for non-thread-safe scenarios. The class includes multiple constructors and methods for adding, removing, and accessing elements, as well as checking size and capacity.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Vector

Int x[]=new int[10]


Vector is like the dynamic array which can grow or shrink its size. Unlike array, we can store
n-number of elements in it as there is no size limit. It is a part of Java Collection framework
since Java 1.2. It is found in the [Link] package and implements the List interface, so we
can use all the methods of List interface here.

It is recommended to use the Vector class in the thread-safe implementation only. If you
don't need to use the thread-safe implementation, you should use the ArrayList, the
ArrayList will perform better in such case.

The Iterators returned by the Vector class are fail-fast. In case of concurrent modification, it
fails and throws the ConcurrentModificationException.

Java Vector class Declaration


public class Vector<E>
extends Object<E>
implements List<E>, Cloneable, Serializable
Java Vector Constructors
Vector class supports four types of constructors. These are given below:

SN Constructor Description

1) Vector() It constructs an empty vector with the default size as 10.

Vector v=new Vector();

2) Vector(int initialCapacity) It constructs an empty vector with the specified initial


capacity and with its capacity increment equal to zero.

Vector v=new Vector(5);

3) Vector(int initialCapacity, int It constructs an empty vector with the specified initial
capacityIncrement) capacity and capacity increment.

Vector v=new Vector(15,5);


Import [Link];

Méthodes de Vector : addElement, insertElementAt, elementAt, setElementAt, removeElementAt, size,


contains, indexOf, lastIndexOf.

Java Vector Methods


The following are the list of Vector class methods:

SN Method Description

1) add() It is used to append the specified element in the given vector.

2) addElement() It is used to append the specified component to the end of this vector. It
increases the vector size by one.

3) capacity() It is used to get the current capacity of this vector.

4) clear() It is used to delete all of the elements from this vector.

5) contains() It returns true if the vector contains the specified element.

6) elementAt() It is used to get the component at the specified index.

7) equals() It is used to compare the specified object with the vector for equality.

8) firstElement() It is used to get the first component of the vector.

9) get() It is used to get an element at the specified position in the vector.

10) indexOf() It is used to get the index of the first occurrence of the specified element
in the vector. It returns -1 if the vector does not contain the element.
11) insertElementAt() It is used to insert the specified object as a component in the given vector
at the specified index.

12) isEmpty() It is used to check if this vector has no components.

13) lastElement() It is used to get the last component of the vector.

14) lastIndexOf() It is used to get the index of the last occurrence of the specified element
in the vector. It returns -1 if the vector does not contain the element.

15) remove() It is used to remove the specified element from the vector. If the vector
does not contain the element, it is unchanged.

16) removeAll() It is used to delete all the elements from the vector that are present in the
specified collection.

17) removeAllElements() It is used to remove all elements from the vector and set the size of the
vector to zero.

18) removeElement() It is used to remove the first (lowest-indexed) occurrence of the argument
from the vector.

19) removeElementAt() It is used to delete the component at the specified index.

20) set() It is used to replace the element at the specified position in the vector
with the specified element.

21) setElementAt() It is used to set the component at the specified index of the vector to the
specified object.

22) setSize() It is used to set the size of the given vector.

23) size() It is used to get the number of components in the given vector.

24) toString() It is used to get a string representation of the vector.

25) trimToSize() It is used to trim the capacity of the vector to the vector's current size.
Java Vector Example

1. import [Link].*;
2. public class VectorExample {
3. public static void main(String args[]) {
4. //Create a vector
5. Vector<String> vec = new Vector<String>();
6. //Adding elements using add() method of List
7. [Link]("Tiger");
8. [Link]("Lion");
9. [Link]("Dog");
10. [Link]("Elephant");
11. //Adding elements using addElement() method of Vector
12. [Link]("Rat");
13. [Link]("Cat");
14. [Link]("Deer");
15. [Link]("Elements are: "+vec);
16. }
17. }

Output:

Elements are: [Tiger, Lion, Dog, Elephant, Rat,


Cat, Deer]

Java Vector Example 2


1. import [Link].*;
2. public class VectorExample1 {
3. public static void main(String args[]) {
4. //Create an empty vector with initial capacity 4
5. Vector<String> vec = new Vector<String>(4);
6. //Adding elements to a vector
7. [Link]("Tiger");
8. [Link]("Lion");
9. [Link]("Dog");
10. [Link]("Elephant");
11. //Check size and capacity
12. [Link]("Size is: "+[Link]());
13. [Link]("Default capacity is: "+[Link]());
14. //Display Vector elements
15. [Link]("Vector element is: "+vec);
16. [Link]("Rat");
17. [Link]("Cat");
18. [Link]("Deer");
19. //Again check size and capacity after two insertions
20. [Link]("Size after addition: "+[Link]());
21. [Link]("Capacity after addition is: "+[Link]());
22. //Display Vector elements again
23. [Link]("Elements are: "+vec);
24. //Checking if Tiger is present or not in this vector
25. if([Link]("Tiger"))
26. [Link]("Tiger is present at the index " +[Link]("Tiger"));
27. else
28. [Link]("Tiger is not present in the list.");
29. //Get the first element
30. [Link]("The first animal of the vector is = "+[Link]());
31. //Get the last element
32. [Link]("The last animal of the vector is = "+[Link]());
33. }
34. }

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

Java Vector Example 3

1. import [Link].*;
2. public class VectorExample2 {
3. public static void main(String args[]) {
4. //Create an empty Vector
5. Vector<Integer> in = new Vector<>();
6. //Add elements in the vector
7. [Link](100);
8. [Link](200);
9. [Link](300);
10. [Link](200);
11. [Link](400);
12. [Link](500);
13. [Link](600);
14. [Link](700);
15. //Display the vector elements
16. [Link]("Values in vector: " +in);
17. //use remove() method to delete the first occurence of an element
18. [Link]("Remove first occourence of element 200: "+[Link]((Int
eger)200));
19. //Display the vector elements afre remove() method
20. [Link]("Values in vector: " +in);
21. //Remove the element at index 4
22. [Link]("Remove element at index 4: " +[Link](4));
23. [Link]("New Value list in vector: " +in);
24. //Remove an element
25. [Link](5);
26. //Checking vector and displays the element
27. [Link]("Vector element after removal: " +in);
28. //Get the hashcode for this vector
29. [Link]("Hash code of this vector = "+[Link]());
30. //Get the element at specified index
31. [Link]("Element at index 1 is = "+[Link](1));
32. }
33. }

Output:

Values in vector: [100, 200, 300, 200, 400, 500, 600, 700]
Remove first occourence of element 200: true
Values in vector: [100, 300, 200, 400, 500, 600, 700]
Remove element at index 4: 500
New Value list in vector: [100, 300, 200, 400, 600, 700]
Vector element after removal: [100, 300, 200, 400, 600]
Hash code of this vector = 130123751
Element at index 1 is = 300

You might also like