Vectors in Java - Learners Pool

Learners Pool

Knowledge that matters

Vectors in Java

Share This

Vectors (the java.util.Vector class) are commonly used instead of arrays, because they expand automatically when new data is added to them. The Java 2 Collections API introduced the similar ArrayList data structure. ArrayLists are unsynchronized and therefore faster than Vectors, but less secure in a multithreaded environment. The Vector class was changed in Java 2 to add the additional methods supported by ArrayList.


Vectors can hold only Objects and not primitive types (eg, int). If you want to put a primitive type in a Vector, put it inside an object i.e, Wrapping
Creation of Vectors
Import either import java.util.Vector; or import java.util.*;. Vectors are implemented with an array, and when that array is full and an additional element is added, a new array must be allocated. Because it takes time to create a bigger array and copy the elements from the old array to the new array, it is a little faster to create a Vector with a size that it will commonly be when full. Of course, if you knew the final size, you could simply use an array. However, for non-critical sections of code programmers typically don't specify an initial size.
A Vector is can be created with the help three constructors

  1. Vector(): creates a vector with default size and grow size ie., 10
  2. Vector(int size): creates a vector with specified size and default grow size as 10.
  3. Vector(int size, inr growsize): creates a vector with specified size specified grow size as 10.

Eg:
Vector  V1=new Vector ();
Vector  V2=new Vector(3);
Vector  V3=new Vector (5,2);

Benefits of Vector over Array
  • Vector stores different type of items
  • Vector can be grown at run time
  • Vector can be shrinked run time

Methods of Vector class

  • int capacity(): This method returns the current capacity of the vector.
  • int size(): It returns the current size of the vector.
  • void addElement(Object element): It inserts the element at the end of the Vector.
  • void setElementAt(Object element, int index): It updates the element of specifed index with the given element.
  • boolean removeElement(Object element): Removes the specifed element from vector.
  • boolean removeElementAt(int pos): Removes the element at the specified position from vector.
  • boolean removeAllElements(): Removes All elements from vector.
  • Object get(int index): Returns the element at the specified index.
  • boolean isEmpty(): This method returns true if Vector doesn’t have any element.
  • int  indexOf(Object element): returns the first occurrence of the specified object in the Vector
  • int  lastIndexOf(Object element): returns the last occurrence of the specified object in the Vector
  • void copyInto(Object [] array): copies all the elements form the vector to an object array.
  • void  trimToSize():Trims the capacity of this vector to be the vector's current size. 
  • void setSize(int size): It changes the existing size with the specified size.
Printing elements in the Vector
Elements of vector can be printed in many ways. Most commonly used methods are

  • Using a for loop

for(int i=0; i<v.size();i++)
System.out.println(v.get(i));

  • Using a for each loop

for(Object  value : v)
System.out.println(value);

  • Directly printing the vector  

System.out.println(v);

No comments:

Post a Comment