Write About Vectors in Java ?

Vectors 

J2SE 5.0 version supports the concept of variable arguments to methods.

v  This can be achieved by using vector class contained in java. Util package.

v  This class can be used to create a generic dynamic array known as vector.

v  Vectors can be hold objects of any type and any number .

v  The objects are having same or different data type.

v  Arrays can be easily implemented as vectors.

Vector v= new Vector( ); without size
Vector v= new Vector(3); with size

Advantages:

v  Vectors are convienent to store objects.

v  Vectors can be used to store list of objects that may vary in size.

v  We can add (or) delete objects from the list as when required.

v  The disadavntage of vector is we cannot directly store simple data types.

Vector class supports number of methods that are used to manipulate vectors:

v. addElement(item)®Add the item of the end of the vector

v.size( ) ®give number of object present

v.removeElement(item) ®remove the specified item from vector

v.removeElementAt(n) ® remove the item stored at nth position

v.elementAt(o) ® give the name of the nth element

v.removeAllElements® remove all elements from vector v

v.capacity ( ) ® gives the total size of the vector

v.lastElement( ) ®gievs the last element name

v.firstElement( ) ® gives the first element name

program:

ENTER DIFFERENT OBJECTS INTO VECTOR

import java.util.*;
class Vect
{
  public static void main(String args[])
   {
      Vector v=new Vector(6);
       System.out.println("SIZE IS"+v.size());
       System.out.println("CAPACITY IS"+v.capacity());
       v.addElement(new Integer(3));
       v.addElement(new Integer(8));
       v.addElement(new Integer(10));
       v.addElement("boss");
      System.out.println("AFTER INSERTING  VECTOR SIZE"+v.size());
      v.addElement(new Float(5.8));
      System.out.println("LAST ELEMENT IS"+v.lastElement());
      System.out.println("FIRST ELEMENT IS"+v.firstElement());
      Enumeration e=v.elements();
      System.out.println("ELEMENTS IN  VECTOR");
      while(e.hasMoreElements())
         {
            System.out.println(e.nextElement());
           }                   
     }
}


OUTPUT:
SIZE IS 0  CAPACITY IS 6
AFTER INSERTING VECTORSIZE 4
LAST ELEMENT IS 5.8
FIRST ELEMENT IS 3
ELEMENTS IN  VECTOR     3
8
10
boss
5.8


Related

Java 2399713876598685896

Post a Comment

emo-but-icon

item