What We Are Learn On This Post
Vector Class In Java: A Vector also stores elements (objects) similar to ArrayList, but vector is synchronized. It means even if several threads act on the Vector object simultaneously, the results will be reliable.
As Vector is synchronized so it is rarely used in the non-thread environment because it will give poor performance in operations like searching, adding, delete, and update of its elements.
The implemented data structure of Vector is a growable array. Vector is one type of legacy class (Those classes are which are coming from the earlier or first version of java and has not been modified), but now it is fully compatible with collections.
Important points To Remember Of Vector
- The underline data structure is a resizeable array or growable array that means it can grow or shrink as expected.
- Like in an array you can also access the vector elements by using the index.
- There are very much similar between ArrayList and Vector, but vector is synchronized; hence, the vector elements are thread-safe.
- Insertion order is preserved
- Duplicates are allowed
- Heterogeneous elements are allowed.
- Null Insertion is possible
- It implements Serializable, Cloneable, and Random Access interface.
Different Way to Create Vector Class object
Vector vec = new Vector();
This will create an empty vector with initial capacity 10, that means you can insert ten elements, and when you try to insert the 11th elements vector automatically doubles its size by 20.
Vector object= new Vector(int initialCapacity);
If you try to initialize the vector with some initial capacity, then you can use the above syntax, and according to the above syntax, it will create a vector object with the mentioned initial capacity.
Vector object= new vector(int initialcapacity, capacityIncrement);
If you are going to create a vector class object by using the above syntax and with two parameters like initialCapacity and capacity increment that means once the insert element number reached to the initial capacity number then the capacity increment will not double it will increment only as of the capacity increment mentioned value.
Vector vec= new Vector(2, 5);
That means when you enter the 3rd number variable that times the vector size increased by 5, so the total size of the vector after increment is 7 (2+5).
Vector Constructor
Like other collection classes, Vector also have constructors and that is:
- Vector(): Creates a default vector of the initial capacity is 10.
- Vector(int size): Creates a vector whose initial capacity is specified by size.
- Vector(Collection c): Creates a vector that contains the elements of collection c.
Vector( Collection<? extends E> c): It constructs a vector that contains the elements of a collection c. - 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 that a vector is resized upward.
Note: If the increment is specified, then vector will expand according to the mentioned size on each allocation cycle, but if the increment is not specified then the vector capacity gets doubled in each allocation cycle.
Vector Methods
Add the different Vector Methods with Example
Vector Program Example
Write a Program to Add Elements in Vector and Retrieve Using Enumeration?
package com.java.Softwaretestingblog; import java.util.Enumeration; import java.util.Vector; public class VectorEnnumaration { public static void main(String[] args) { // TODO Auto-generated method stub Vector<String> vct = new Vector<String>(); //adding elements to the end vct.add("First"); vct.add("Second"); vct.add("Third"); vct.add("Random"); Enumeration<String> enm = vct.elements(); while(enm.hasMoreElements()) { System.out.println(enm.nextElement()); } } }
Execution: Check the output link
Output:
First Second Third Random
Write A Program to Add & Retrieve Vector Element?
package com.java.Softwaretestingblog; import java.util.Iterator; import java.util.Vector; public class VectorIterator { public static void main(String[] args) { // TODO Auto-generated method stub Vector<String> vct = new Vector<String>(); //adding elements to the end vct.add("First"); vct.add("Second"); vct.add("Third"); vct.add("Random"); Iterator<String> itr = vct.iterator(); while(itr.hasNext()) { System.out.println(itr.next()); } } }
Execution: learn link
Output:
First Second Third Random
Write a Program For Basic Vector Operations In Java With Program?
package com.java.Softwaretestingblog; import java.util.Vector; public class BasicVectorOperations { public static void main(String[] args) { // TODO Auto-generated method stub Vector<String> vct = new Vector<String>(); //adding elements to the end vct.add("First"); vct.add("Second"); vct.add("Third"); System.out.println(vct); //adding element at specified index vct.add(2,"Random"); System.out.println(vct); //getting elements by index System.out.println("Element at index 3 is: "+vct.get(3)); //getting first element System.out.println("The first element of this vector is: "+vct.firstElement()); //getting last element System.out.println("The last element of this vector is: "+vct.lastElement()); //how to check vector is empty or not System.out.println("Is this vector empty? "+vct.isEmpty()); } }
Execution: The output link
Output:
[First, Second, Third] [First, Second, Random, Third] Element at index 3 is: Third The first element of this vector is: First The last element of this vector is: Third Is this vector empty? false
Write a Program to Retrieve Vector Elements Using Enumeration?
package com.java.Softwaretestingblog; import java.util.Enumeration; import java.util.Vector; public class MyEnumeration { public static void main(String[] args) { // TODO Auto-generated method stub Vector<String> lang = new Vector<String>(); Enumeration<String> en = null; lang.add("JAVA"); lang.add("JSP"); lang.add("SERVLET"); lang.add("EJB"); lang.add("PHP"); lang.add("PERL"); en = lang.elements(); while(en.hasMoreElements()) { System.out.println(en.nextElement()); } } }
Execution: The output link
Output:
Java Unix Oracle C++ Perl
Ref: article
Leave a Reply