Java Reference
In-Depth Information
names.add(aName);
This adds a reference to the object aName to the Vector<> object called names . The new entry is added at
the end of the vector, and the size is increased by one. All the objects that were already stored in the vector
remain at their previous index position.
You can also store an object at a particular index position using another version of add() . The first argu-
ment is the index position and the second is the object to be stored. The index must be less than or equal to
the size of the vector, which implies that either there is already an object reference at this position, or it is
the position at the end of the vector that is next in line to receive one. The index is the same as for an array
— an offset from the first element — so you reference the first element using an index value of zero. For
example, to insert the object aName as the third entry in names , you would write the following:
names.add(2, aName);
The index is of type int and represents the position of the new object. The new object, aName , is inserted
in front of the object that was previously at index position 2, so objects stored at index values equal to or
greater than 2 are shifted along so their indexes increase by 1. If you specify an index argument that is neg-
ative, or greater than the size of the vector, an ArrayIndexOutOfBoundsException is thrown.
To replace an element in a vector or array list you use the set() method. This accepts two arguments.
The first is the index position where the object specified by the second argument is to be stored. To change
the third element in the vector names to newName , you write:
names.set(2, newName);
The method returns a reference to the object that was previously stored at this position. This gives you a
chance to hang on to the displaced object if you want to keep it. If the first argument is negative, or is greater
than or equal to the current size of the vector, an ArrayIndexOutOfBoundsException is thrown.
You can add all the objects from another collection, either at the end or following a given index position.
For example, to append the contents of a LinkedList<> object, myNamesList — and here I'm referring to
the java.util.LinkedList<> type, not the homemade version — to a vector, names , you would write:
names.addAll(myNamesList);
The parameter to the method is of type Collection<? extends T> , so because the names vector
is of type Vector<String> , the argument must be of type Collection<String> . Here, this implies that
myNamesList is of type LinkedList<String> .
To insert the collection of objects at a particular position , you specify the insertion position as the first
argument. So to insert the objects from myNamesList in the names vector starting at index position i , you
would write:
names.addAll(i, myNamesList);
The objects originally at and following index position i are all shifted along to make room for the new
objects. If the index value passed as the first argument is negative, or is greater than the size of names , an
ArrayIndexOutOfBoundsException object is thrown. Adding a collection increases the size of the vector
by the number of objects added.
Search WWH ::




Custom Search