Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Click Here!
Click Here!
ITKnowledge
Find:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


Inserting and removing elements

There are two methods to put a new object into a vector, addElement() and insertElementAt(). Their signatures are

     public final void addElement(Object  o)
     public final void insertElementAt(Object  o, int  index)

addElement() places the object at the end of the Vector. This method takes essentially constant time as long as there’s empty space left in the vector. It takes slightly longer if the Vector first needs to be grown.

insertElementAt() places the object at the specified position in the vector. All elements of the vector at or beyond the specified index are moved up one to make room for the newcomer. The efficient native method System.arraycopy() is used to move the remaining elements up, but it’s still less than ideal. If a program requires frequent insertions into the middle of a list, you may well be better off writing your own linked list class.

You can also replace elements in vectors. Because the size of the vector stays the same, this is always quite fast. The relevant method is:

     public final void setElementAt(Object  o, int  index)

The object that was previously at index is no longer there. Instead, it is replaced by the new Object o. If there are no other references to the old object, then it will eventually be garbage-collected.

Elements can also be removed from a vector. This generally requires moving elements down that were above the object. Again this can be done with the System.arraycopy() method, but if you’re doing a lot of this, you should consider using a linked list instead. The method is

     public final void removeElementAt(int  index)

You can also remove an object from a Vector without knowing its index. The method to do this is

     public final boolean removeElement(Object  o)

However, this method requires Java to traverse the entire Vector, searching for the requested object. Thus, its execution time is proportional to the length of the array. In computer science terms, this is an O(n) (pronounced “order en”) operation where n is the number of elements in the Vector.

Furthermore, only the first occurrence of the object in the Vector is removed. This may or may not be what you expect. Removing all references to the object from the Vector requires multiple passes through the vector. If the object is found and removed, removeElement() returns true. Otherwise, it returns false. You can remove all references to an object o from a Vector v in one line of code like this:

     while (v.removeElement(o)) {;}

However, this is deceptively simple. If there are n references to o in v, then this single line of code requires n+1 passes through the vector to remove them all.

The removeAllElements() method deletes every element in the Vector. Its signature is

     public final void removeAllElements()

In the current implementation, Java deletes all elements from a Vector by looping through the Vector and setting each element to null. This is an O(n) operation, but it’s less than optimally efficient. It looks something like this:

     for (int j = 0; j < elementCount; j++) elementData[j] = null;
     elementCount = 0;

Other implementations are possible. For example, you could simply reallocate a new elementData array like this:

     elementData = new Object[elementData.length];
     elementCount = 0;

This only takes constant time; this is an O(1) operation.

You could even leave the elementData array untouched and change only the elementCount field. This is even quicker. The old elementData array components will simply be overwritten as necessary. However, this has the potential to cause memory leaks because references to the old components still exist in the array, and thus those objects will not be garbage-collected.

Finding Objects in Vectors

In addition to the above methods for manipulating the contents of a Vector, there are several methods to find objects in a Vector. The elementAt(int i) method returns a reference to the Object at index i in the Vector. If i is not a valid index into the Vector, an ArrayIndexOutOfBoundsException is thrown.

     public final Object elementAt(int  i)

The firstElement() and lastElement() methods return references to the first and last elements in the Vector respectively. If the Vector has no elements, and thus no first or last element, a NoSuchElementException is thrown.

     public final Object firstElement()
     public final Object lastElement()

Five methods search for a particular object. Because this requires a traversal of the array, these methods are proportional to the number of elements in the Vector. These methods are:

     public final boolean contains(Object  o)
     public final int indexOf(Object  o)
     public final int lastIndexOf(Object  o)

The contains() method returns true if o is in the Vector and false if it isn’t. The indexOf() and lastIndexOf() methods return the first and last indices of the specified object in the Vector respectively. If the object is not found in the Vector, then -1 is returned.

You can pass an extra int to these methods to indicate the element at which to begin searching.

     public final int indexOf(Object  o, int  index)
     public final int lastIndexOf(Object  o, int  index)

The indexOf() method searches forward beginning at that index, and lastIndexOf() searches backward beginning at that index. An ArrayIndex OutOfBoundsException is thrown if the index is less than zero or greater than or equal to the number of elements in the Vector.

Miscellaneous methods

There are three other useful methods in the Vector class. The isEmpty() method returns true if the Vector has no elements and false if it has one or more elements.

     public final boolean isEmpty()

The setSize() method either shrinks or expands the Vector to a given non-negative size. If the vector is expanded, then the new elements are set to null. If the vector is shrunk, then elements past the requested size are removed from the Vector.

     public final void setSize(int  newSize)

Finally, the elements() method returns a reference to an object which implements the Enumeration interface. This provides a convenient way for external classes to process each and every element of the Vector. For example,

     Enumeration e = v.elements();
     while (e.hasMoreElements()) {
      Object o  = e.nextElement();
      // work with o
     }


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.