- vector is a one kind of dynamic array.
- default capacity of vector is 10.
- when overflow the vector class (Greater than) than its grow with double capacity.
- Vector is synchronized.
- it has a capacity() to ensure the vector capacity.
- Vector contains some spacial methods that not a part of collection framework.
- Vector proves to be very useful if you don't know the size of the array in advance
- you need to import java.util.*;
NOTE : if equal capacity and size of elements than not grow.
Constructors :
Constructors | |
---|---|
Vector() | create a vector with initial capacity 10. |
Vector(int size) | create a vector with one parameter size is capacity you can define. |
Vector(int size, int incr) | create a vector with initial capacity and increement value when overflow it that you can define |
Vector(Collection c) | create a vector contains the Collection. |
Methods :
Methods | |
---|---|
void add(int index, Object element) | you can add element at spacific index |
boolean addAll(int index, Collection c) | add Collection start at spacific index. |
void addElement(Object obj) | add element at last position of vector class. |
int capacity() | it returns the capacity (size) of an vector. |
void clear() | it is used to clear the vector. |
boolean contains(Object elem) | check wather elements is contains in vector. |
boolean containsAll(Collection c) | check wather entire collection in vector. |
Enumeration elements() | return enumeration of vector elements. |
Object get(int index) | get element by spacifying index. |
boolean isEmpty() | check wather the vector is empty or not. |
Object remove(int index) | remove element by giving index of elements. |
boolean remove(Object o) | remove element by giving object as parameter. |
Object set(int index, Object element) | you can replace element by spacifying position. |
List subList(int fromIndex, int toIndex) | create a sublist of inclusive fromIndex to toIndex. |
Object[] toArray() | return an array. |
void trimToSize() | Trims the capacity of this vector to be the vector's current size. |
public int size() | returns number of elements in vector. |
Example of Vector class.
// Vector class
// class B that extends A and implements C
class VectorExample{ public static void main(String... str){ // create a empty vector has default capacity is 10. Vector v=new Vector(); // first we check initial capacity of vector class System.out.println("Capacity : "+v.capacity()); // add one String object v.addElement("abc"); // add one int value v.addElement(45); // add one float value v.add(22.22); // check vector class capacity System.out.println("Capacity : "+v.capacity()); // add another 7 values to check capacity for(int a=0;a<7;a++){ v.add(a+12); } // check vector class capacity System.out.println("Capacity : "+v.capacity()); // now remove one v.remove(22.22); // check capacity System.out.println("Capacity : "+v.capacity()); // enumeration for print each elements of vector class // v.elements() is method of vector class that return enumeration Enumeration venum = v.elements(); while(venum.hasMoreElements()) System.out.print(venum.nextElement() + " "); // now clear vector class v.clear(); // print size of vector class System.out.println("Size : "+v.size()); } }