Thursday, May 19, 2011

Common methods in Vector API of Java

There are many useful methods in the Vector class and its parent classes. Here are some of the most useful. v is a Vector, i is an int index, o is an Object.
Method Description
v.add(o) adds Object o to Vector v
v.add(i, o) Inserts Object o at index i, shifting elements up as necessary.
v.clear() removes all elements from Vector v
v.contains(o) Returns true if Vector v contains Object o
v.firstElement(i) Returns the first element.
v.get(i) Returns the object at int index i.
v.lastElement(i) Returns the last element.
v.listIterator() Returns a ListIterator that can be used to go over the Vector. This is a useful alternative to the for loop.
v.remove(i) Removes the element at position i, and shifts all following elements down.
v.set(i,o) Sets the element at index i to o.
v.size() Returns the number of elements in Vector v.
v.toArray(Object[]) The array parameter can be any Object subclass (eg, String). This returns the vector values in that array (or a larger array if necessary). This is useful when you need the generality of a Vector for input, but need the speed of arrays when processing the data.

No comments:

Post a Comment

Chitika