VECTORS
     - Ankita R Karia
Vectors
   Vector implements a DYNAMIC ARRAY.

   Vectors can hold objects of any type and any number.

   Vector class is contained in java.util package

   Vector is different from ARRAY in two ways:-

    1.   Vector is synchronized.

    2.   It contains many legacy methods that are not part of the
         collection framework.                          1. Enumeration.
                                                        2. Iterator
Declaring VECTORS
Vector list = new Vector();                 Creates a default vector, which
                                            has an initial size 10.
                                                Creates a vector, whose
Vector list = new Vector(int size);             initial capacity is specified
                                                by size.


Vector list = new Vector(int size, int incr);


                Creates a vector, whose initial capacity is specified
                by size and whose increment is specified by incr.
Vectors (Contd….)
   A vector can be declared without specifying any size explicitly.

   A vector without size can accommodate an unknown number of
    items.

   Even when size is specified, this can be overlooked and a different
    number of items may be put into the vector



     In contrast, An ARRAY must be
     always have its size specified.
WRAPPER CLASSES
   Vectors cannot handle primitive data types like int, float, long, char
    and double.
   Primitive data types may be converted into objects using the
    WRAPPER classes contained in java.lang package.

                    SIMPLE TYPE         WRAPPER CLASSES
            boolean                   Boolean
            char                      Character
            double                    Double
            float                     Float
            int                       Integer
            long                      Long
Converting Primitive data
              types
           to Objects.
         SYNTAX                                 ACTION
Integer ob=new Integer(i)   Converts primitive integer to Integer object
Float ob=new Float(f)       Converts primitive float to float object


          Converting Objects to
          Primitive data types.
          SYNTAX                                ACTION
int i=ob.intValue()         Converts Integer object to primitive integer
float f=ob.floatValue()     Converts Float object to primitive float
Converting Numbers to
                Strings
        Using toString() method
          SYNTAX                             ACTION
str=Integer.toString(i)   Converts primitive integer to string
str=Float.toString(f)     Converts primitive float to string




                           There are other wrapper classes
                           methods Refer Balagurusamy
VECTOR METHODS
void addElement(Object element)    The object specified by element is
                                   added to the vector
int capacity()                     Returns the capacity of the vector

boolean contains(Object element)   Returns true if element is
                                   contained by the vector, else false
void copyInto(Object array[])      The elements contained in the
                                   invoking vector are copied into the
                                   array specified by array[]
elementAt(int index)               Returns the element at the location
                                   specified by index
Object firstElement().             Returns the first element in the
                                   vector
VECTOR METHODS
void insertElementAt(Object             Adds element to the vector at the
element, int index)                     location specified by index

boolean isEmpty()                       Returns true if Vector is empty,
                                        else false
Object lastElement()                    Returns the last element in the
                                        vector
void removeAllElements()                Empties the vector. After this
                                        method executes, the size of vector
                                        is zero.
void removeElementAt(int index)         Removes element at the location
                                        specified by index
void setElementAt(Object element, int   The location specified by index is
index)                                  assigned element
VECTOR METHODS
void setSize(int size)   Sets the number of elements in the
                         vector to size. If the new size is less
                         than the old size, elements are lost.
                         If the new size is larger than the
                         old, null elements are added

int size()               Returns the number of elements
                         currently in the vector
ENUMERATION INTERFACE
                                    OBTAIN ONE
                                      AT TIME
   Defines the methods by which you can
    enumerate the elements in a collection of objects


         boolean hasMoreElements()       Object nextElement()




         Returns true while there are     Returns the next object in
        still more elements to extract          enumeration
              and false otherwise
PROGRAMMING EXAMPLE

 Write a program that accepts a shopping list of
  five items from the command line and stores
  them in a vector.
 Modify the program to accomplish the
  following:-
1. To delete an item in the list.
2. To add an item at a specified position in the
    list.
3. To add an item at the end of the list.
4. To print the contents of the vector.

Vectors in Java

  • 1.
    VECTORS - Ankita R Karia
  • 2.
    Vectors  Vector implements a DYNAMIC ARRAY.  Vectors can hold objects of any type and any number.  Vector class is contained in java.util package  Vector is different from ARRAY in two ways:- 1. Vector is synchronized. 2. It contains many legacy methods that are not part of the collection framework. 1. Enumeration. 2. Iterator
  • 3.
    Declaring VECTORS Vector list= new Vector(); Creates a default vector, which has an initial size 10. Creates a vector, whose Vector list = new Vector(int size); initial capacity is specified by size. Vector list = new Vector(int size, int incr); Creates a vector, whose initial capacity is specified by size and whose increment is specified by incr.
  • 4.
    Vectors (Contd….)  A vector can be declared without specifying any size explicitly.  A vector without size can accommodate an unknown number of items.  Even when size is specified, this can be overlooked and a different number of items may be put into the vector In contrast, An ARRAY must be always have its size specified.
  • 5.
    WRAPPER CLASSES  Vectors cannot handle primitive data types like int, float, long, char and double.  Primitive data types may be converted into objects using the WRAPPER classes contained in java.lang package. SIMPLE TYPE WRAPPER CLASSES boolean Boolean char Character double Double float Float int Integer long Long
  • 6.
    Converting Primitive data types to Objects. SYNTAX ACTION Integer ob=new Integer(i) Converts primitive integer to Integer object Float ob=new Float(f) Converts primitive float to float object Converting Objects to Primitive data types. SYNTAX ACTION int i=ob.intValue() Converts Integer object to primitive integer float f=ob.floatValue() Converts Float object to primitive float
  • 7.
    Converting Numbers to Strings Using toString() method SYNTAX ACTION str=Integer.toString(i) Converts primitive integer to string str=Float.toString(f) Converts primitive float to string There are other wrapper classes methods Refer Balagurusamy
  • 8.
    VECTOR METHODS void addElement(Objectelement) The object specified by element is added to the vector int capacity() Returns the capacity of the vector boolean contains(Object element) Returns true if element is contained by the vector, else false void copyInto(Object array[]) The elements contained in the invoking vector are copied into the array specified by array[] elementAt(int index) Returns the element at the location specified by index Object firstElement(). Returns the first element in the vector
  • 9.
    VECTOR METHODS void insertElementAt(Object Adds element to the vector at the element, int index) location specified by index boolean isEmpty() Returns true if Vector is empty, else false Object lastElement() Returns the last element in the vector void removeAllElements() Empties the vector. After this method executes, the size of vector is zero. void removeElementAt(int index) Removes element at the location specified by index void setElementAt(Object element, int The location specified by index is index) assigned element
  • 10.
    VECTOR METHODS void setSize(intsize) Sets the number of elements in the vector to size. If the new size is less than the old size, elements are lost. If the new size is larger than the old, null elements are added int size() Returns the number of elements currently in the vector
  • 11.
    ENUMERATION INTERFACE OBTAIN ONE AT TIME  Defines the methods by which you can enumerate the elements in a collection of objects boolean hasMoreElements() Object nextElement() Returns true while there are Returns the next object in still more elements to extract enumeration and false otherwise
  • 12.
    PROGRAMMING EXAMPLE  Writea program that accepts a shopping list of five items from the command line and stores them in a vector.  Modify the program to accomplish the following:- 1. To delete an item in the list. 2. To add an item at a specified position in the list. 3. To add an item at the end of the list. 4. To print the contents of the vector.