JAVA- Vectors

Vectors are created like the arrays but vector can hold the objects. And these objects can be of any type and size. In array the elements are must be homogeneous, but in the vectors we can implement the list as without homogenous, and thus the vectors are dynamic array. Whenever we required we can easily add or delete objects. The java.util package contained the vector class.

Creation of vector is as follows:

Syntax:            vector vector-name = new vector();

We can give the size in the declaration which is as follows:

vector vector-name = new vector(5);

e.g:      vector objlist = new vector(5);

There are many methods which are used to manipulate the vectors these are as follows:

Methods                                             working

size()                                                    returns the number of objects.

addElement(object element)              the object is added to the vector at the last position.

removeElement(object element)       removes the element which specified.

elementAt(element-number)            returns the object name as the specified.

removeAllElements()                          removes all the elements from the list.

insertElementAt(element,location)    inserts the element at specified location.

copyInto(array)                                   copy all the elements from the list to array.

Leave a Reply

Your email address will not be published. Required fields are marked *