Ads

Wednesday, 1 November 2017

java.util.Vector class in java

Vector Class


The vector class creates a dynamic array.

The simple array you create is of a fixed size, but you can create the dynamic array using vector.

Thus, a vector automatically when needed.

In the vector, you can insert objects but in the simple array you can store only elements with the same data type.

The Vector class is similar to a traditional Java array, except that it can grow as necessary to accommodate new elements.

Note :
 

By default size of the dynamic array is 10

Constructors


1. Vector()
This constructor creates a default vector, which has an initial size of 10

2.Vector(int size)
This constructor accepts an argument that equals to the required size and creates a vector whose initial capacity is specified by size


3.Vector(int size, int incr)
This constructor creates a vector whose initial capacity is specified by size and whose increment is specified by incr. The increment specifies the number of elements to allocate each time that a vector is resized upward


Methods


void addElement(Object obj)
 
This method is used to Adds the specified component to the end of this vector, increasing its size by one.


int capacity() 

This method Returns the current capacity of this vector.

int size()

This method Returns the number of components in this vector.


void ensureCapacity(int minCapacity)

Increases the capacity of this vector, if necessary, to ensure that it can hold at least the number of components specified by the minimum capacity argument.


void setSize(int newSize)

This method is used to Sets the size of this vector.


Object firstElement()

Returns the first component (the item at index 0) of this vector.


int lastIndexOf(Object elem)

Returns the index of the last occurrence of the specified object in this vector.


Object elementAt(int index)

Returns the component at the specified index.


int lastIndexOf(Object elem)

Returns the index of the last occurrence of the specified object in this vector.


void insertElementAt(Object obj, int index)

Inserts the specified object as a component in this vector at the specified index.


boolean remove(Object o)

Removes the first occurrence of the specified element in this vector, If the vector does not contain the element, it is unchanged.


boolean removeElement(Object obj)

Removes the first (lowest-indexed) occurrence of the argument from this vector.


void removeAllElements()

Removes all components from this vector and sets its size to zero.


boolean contains(Object elem)

Tests if the specified object is a component in this vector.


void copyInto(Object[] anArray)

Copies the components of this vector into the specified array.


Enumeration elements()

Returns an enumeration of the components of this vector.


boolean isEmpty()

Tests if this vector has no components.


Example


import java.util.*;
class VectDemo
{
    public static void main(String args[])

    {
        Vector v=new Vector();

        System.out.println(Default Capacity:"+v.capacity());
        v.addElements("TheJavaXpert");
        System.out.println("Vector Element:"+v);
        System.out.println("IsEmpty:"+v.IsEmpty);
       
        Vector v2=new Vector();
        System.out.println("Default Capacity:"+v2.capacity());
        System.out.println(Default Capacity:"+v2.capacity());
    }
}      

       


 
Post a Comment

Click Here