Vector in Java


Prev Tutorial Next Tutorial

Vector in Java

It is one of the Legacy Collection framework class. Vector class object organizes the data in the form of cells. Creating a Vector is nothing but creating an object of Vector class.

Syntax

Vector v=new Vector();

Vector Important Points

  • The default capacity of the Vector v=10 cells
  • Vector are Synchronized.
  • Vector uses Enumeration interface to traverse the elements. It can be use Iterator also.
  • Vector is one of the Legacy Collection framework class.
  • Vector class object organizes the data in the form of cells.
  • The default size of the Vector=0 (size is nothing but number of values available in the cells)
  • In Vector cell values are storing in Heap Memory and cell address are storing in associative memory.

Constructor of vector

  • Vector(): Vector() is used for creating an object of Vector without specifying any number of cells to be created.
  • Vector(int): Vector(int) is used for creating an object for Vector class by specifying number of cells to be created.

Methods of Vector

  • public int capacity(): are used for find the capacity of Vector.
  • public int size(): are used for find size of Vector class object.
  • public void addElement(Object): are used for adding the elements to the Vector one-by-one.
  • public void addElementAt(int.Object): are used for adding the elements to the Vector at specific existing position.
  • public Object removeElementAt(int): are used for removing the elements of Vector on the basics of the position.
  • public void removeElement(Object): are used for removing the elements of Vector on the basics of content.
  • public Enumeration elements(): are used for extracting all the elements of Vector class object

Syntax

Vector v=new Vector();
System.out.println("capacity of v="+v.capacity()); // 10
System.out.println("size of v="+v.size());               // 0
v.addElement(10);
v.addElement(10.75f);
System.out.println(v);      // [10,10.75]
v.addElementAt(1,'A');
System.out.println(v);    // [10,A,10.75]
Object obj=v.removeElementAt(1);
System.out.println(obj);                        // A
v.removeElement('A');
System.out.println(v);    // [10,10.75]

Example of Vector

import java.util.*;

class VectorDemo
{
 public static void main(String [] args)
 {
 Vector v=new Vector();

// Add data to v
v.addElement("Deo");
v.addElement("Smith");
v.addElement("Karter");
v.addElement("Porter");

// Extract the data
  Enumeration e=v.elements();  
  while(e.hasMoreElements()){  
  System.out.println(e.nextElement());  
  }  
 }
}

Output

Deo
Smith
Karter
Porter

Prev Tutorial Next Tutorial

Advertisements




Facebook Likes

Advertisements