Vector implements List

Vector implements List, RandomAccess:- This class is roughly equivalent to ArrayList except it is Synchronized.

Syntax of some important method of Vector class. Given below..

add(Object o):- It adds the element in the end of the Vector.
 
elements():- It returns an enumeration of the element.

elementAt(int index):- It returns the element at the specified index.

firstElement():- It returns the first element of the vector.

lastElement():- It returns last element.

removeElementAt(int index):- It deletes the element from the given index.
 
size():- It returns total number of components available in vector.
import java.util.Vector;

public class VectorExample {
    public static void main(String[] args) {
        Vector<string> vector = new Vector<string>();
        vector.add("test1");
        vector.add("test2");
        vector.add("test3");
        vector.add("test4");
        vector.add("test5");
        // Print all the objects in vector
        System.out.println(vector);

        // Itreate all the objects in vector one by one
        for (String string : vector) {
            System.out.println("The value is " + string);
        }
        // Get object at index 4
        System.out.println("Object at index 4 is " + vector.get(4));

        // Get the size of a vector
        System.out.println("Size of vector is " + vector.size());

        // Get the array from list
        Object[] array = vector.toArray();
        System.out.println("The array of vector is " + array);
    }
}
Output is:

[test1, test2, test3, test4, test5]
The value is test1
The value is test2
The value is test3
The value is test4
The value is test5
Object at index 4 is test5
Size of vector is 5
The array of vector is [Ljava.lang.Object;@1372a1a

People who read this post also read :



1 comments:

Perhaps you could add some information which is not in the Javadoc or your example for ArrayList already.

Sometimes quality is more important than quantity.

Post a Comment

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More