Java Tutorial



Its functionality is exactly similar to ArrayList but Vector class belongs to synchronized whereas ArrayList belongs to non-synchronized class.

Creating a Vector is nothing but creating an object of java.util.Vector class.

Vector API:


Vector (); -- 1
Vector (int size); -- 2

Instance methods:

public int size ();-- 3
public void addItem (Object obj); [old] -- 4
public void addElement (Object obj); [new] -- 5 
public void addItem (int pos, Object obj); -- 6
public void addElement (int pos, Object obj); -- 7 
public Object getItem (int pos); -- 8
public Object remove (int pos); -- 9
public void remove (Object obj ); -- 10 
public void removeAll ();-- 11
public Enumeration elements ();-- 12

The methods 4,5,6 and 7 are used for adding an object to the vector either at end or at specified position. Method-12 is used for extracting the data from vector object.

Enumeration en=v.elements ();
While (en.hasMoreElements())
    Object obj=en.nextElement ();

Write a java program which listed the concept of Vector?


import java.util.*;

class vector {

    public static void main(String[] args) {
        Vector v = new Vector();
        v.addElement(new Integer(10));
        v.addElement(new Float(100.37f));
        v.addElement(new Boolean(true));
        System.out.println("SIZE = " + v.size());
        System.out.println("CONTENTS = " + v);
        Enumeration en = v.elements();
        while (en.hasMoreElements()) {
            Object val = en.nextElement();

Methods in Enumeration method:

public boolean hasMoreElements (); -- 1
public Object nextElement(); -- 2

Method-1 is used for checking weather we have next elements or not. This method returns false when we have next element otherwise it returns false. Method-2 is used for obtaining next element. Method-2 can be used as long as method-1 returns true .


Stack is the sub-class of Vector class. The basic working principal of Stack is Last In First Out.

Stack API:


Stack ();
Stack (int size);

Instance methods:

public boolean empty ();-- 1 
public void push (Object); -- 2 
public Object pop (); -- 3 
public Object peek (); -- 4
public int search (Object); -- 5

Method-1 returns true when the Stack does not contain any elements otherwise false. Method-2 is used for inserting an object into the Stack. Method-3 is used to remove top most elements permanently. Method-4 is used to retrieve top most elements without removing. Method- 5 returns relative position of the element, if it found otherwise returns -1.

Write a java program which illustrates the concept of Stack?


import java.util.*;

class stack {

    public static void main(String[] args) {
        Stack st = new Stack();
        System.out.println("IS STACK EMPTY ? " + st.empty());
        st.push(new Integer(10));
        st.push(new Integer(20));
        st.push(new Integer(30));
        st.push(new Integer(40));
        System.out.println("TOP MOST ELEMENT = " + st.peek());
        System.out.println("DELETED ELEMENT = " + st.pop());
        System.out.println("MODIFIED STACK = " + st.peek());
        System.out.println("IS 10 FOUND ? " + Integer(10)));
        Enumeration en = st.elements();
        while (en.hasMoreElements()) {
            Object obj = en.nextElement();