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

Explain Java Environment?

Java environment includes a large number of development tools are part of the system known as Java development kit is the classes is methods are part of the Java sta...

Explain Java Command Line Arguments?

The way of providing input at the time of exection is known as command line arguments. Command line arguments are parameters that are supplied to the application program at...

Explain Java Tokens?

A Java program is basically a collection of classes. A class is defined by a set of declaration statements and methods containing executable statements. ...

Post a Comment

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

item