![]() |
![]() |
Java Interface | |
Java Class vs Interface | |
Java Multiple Inheritance | |
Java Nested Interface |
Java Packages | |
Java Built in packages | |
Java Package Access Specifiers |
Java Programs | |
Java Keywords Dictionary | |
Java Interview Questions |
The Vector class provides "Arrays" of variable sizes. Vector is very much beneficial than Arrays.
Vector have more benefits than Array.
Vector | Array |
---|---|
Vector is like a array but holds multiple objects of different datatypes. | Array stores only similar data elements. |
Vector does not have any fixed size. No need to specify while declaration. | Array have fixed size. |
Vector increases it's size automatically when the new objects are added. | Array never increases from it's fixed size. It results in overflow error. |
Vector does not waste the memory space. | Array causes more wastage of memory space than vector. |
Vector have several methods for performing operations in easier way. | Array does not have methods to perform operations like vector. |
Vector cannot directly store the values. We need wrapper classes for converting into vector object. | Array directly stores the values of same type. Array does not need any classes like vector. |
Vector methods are add(), clear(),get(),size() etc |
Array methods are toString(),wait(),equals() etc |
Vector is created like arrays like without specifieng size or with size.
Vector class is stored inside the package java.util.Vector
We need to import it before using in the program.
Vector class has three constructors for creation.
Vector vectorName = new Vector(); or Vector vectorName = new Vector(size); or Vector vectorName = new Vector(initialCapacity, inCapacityIncrement);
Following example shows how to create vector
import java.util.Vector; class VectorTest { public static void main(String args[]) { Vector v1 = new Vector(); // empty vector of unknown items Vector v2 = new Vector(15); // initial size of 15 Vector v3 = new Vector(10,1); // grows by 1 } }
Vector provides serveral methods for performing operations very quickly.
Assume vector is declared as Vector v = new Vector();
Method Name | Description |
---|---|
v.add(object) | This method adds an object to the Vector v. |
v.addElement(element) | This method adds a new element at the end of list. |
v.size() | This method gives the size of the Vector object.(total inserted elements) |
v.removeElement(element) | This method removes the specified element from the vector. |
v.removeElementAt(index) | This method removes the specified element from given index. |
v.removeAllElementAt() | This method removes all the present elements from the vector. |
v.insertElementAt(element, index) | This method inserts the new element at specified index. |
v.elementAt(index) | This method gets the element from given index. |
v.clear() | This method removes all the elements from vector object. |
v.contains(object) | This method returns boolean true if given object is present in the vector. |
v.lastElement() | This method gets the last element from the vector. |
v.remove(index) | This method removes the element from given index. It also shifts all elements to down. |
v.set(index,newElement) | This method replaces given index element with new index element. |
import java.util.Vector; class VectorOperations { Vector v = new Vector(); void insert() { v.addElement(new Integer(20)); v.addElement(new Float(3.14f)); v.addElement(new Double(9.818283)); v.addElement(new String("AndroidBerry.com")); } void delete() { v.removeElementAt(2); } void display() { for(int i=0;i<v.size();i++) System.out.println("vector of "+i+" = "+v.elementAt(i)); } public static void main(String args[]) { VectorOperations n = new VectorOperations(); n.insert(); n.display(); System.out.println("\nAfter deleting 2nd element\n"); n.delete(); n.display(); } }