Pages

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.

Disclaimer

Java Jazzle is for learning and training only. Its not guaranteed that content published is correct. The risk from using it lies entirely with the user. Also the opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

About this blog

This is my tech blog. I write on this blog to save my searching stuff on google. It acts as my log of all the stuff I went through and saves my time, because I don't have to search again and again. Another reason for putting stuff on it was, when I am kind of free, it kind of kills it.