Vector in Java are array lists that are used instead of arrays, as they have extending properties, which means that they can grow and shrink to accommodate data even after the Vector has been created. Capacity of vector is always to the lowest degree. Size of the vector is however larger because components are added to the vector at run-time.
Vector in Java are array lists that are used instead of arrays, as they have extending properties, which means that they can grow and shrink to accommodate data even after the Vector has been created.
Capacity of vector is always to the lowest degree. Size of the Vector is however larger because components are added to the vector at run-time.
Integer index access the component in vector class. Vectors implement the Enumeration interface, which makes the contents of a Vector traversed.
Vector instances can grow dynamically. If they reach the maximum limit, resources are allocated dynamically and size gets increased further.
Vector maintains its storage size by capacity Increment.
Here is how to declare a Vector in Java Program:
This Syntax is used to declare an empty Vector r (internal data array size-10, standard storage capacity-0):
public Vector()
This Syntax constructs an empty vector specified by initial capacity:
vector(int initial capacity)
This Syntax creates an empty vector with the specified initial capacity and capacity increment:
vector(int initial capacity,int capacity increment)
Following are the Methods used in Vector:
- void add(int index, Object element): This method inserts a specific element at a specific position.
- boolean add(object o): This method Appends a specific element to the end of the Vector.
- int capacity( ): This method returns the current capacity of the vector
- void clear( ): This method removes all of the elements from the Vector
- void setsize(int new size): This method sets the new size of Vector.
- int size( ): This method returns the number of Component in Vector.
- Vector trim to size( ): This method trims the Capacity of Vector to vector's present size.
- public int lastindexof(obj abc): This method returns the occurrence of last index of the specified object in this vector.
Vector java Example:
package Toturial; import java.util.Iterator; import java.util.Vector; public class VectorExample { public static void main(String[] args) { Vector vt = new Vector(); vt.add("A"); vt.add("RoseIndia"); vt.add("Hello,Hi"); vt.add(123); vt.add(123.45); vt.removeElement("A"); // We have create removeElement method Iterator it = vt.iterator(); while (it.hasNext()) System.out.println(it.next()); } }
OutPut:-
RoseIndia
Hello,Hi
123
123.45