Java Reference
In-Depth Information
Display 16.7
Methods in the Classes ArrayList<T> and Vector<T> (part 2 of 4)
public Vector()
Creates an empty vector with an initial capacity of 10. When the vector needs to increase its
capacity, the capacity doubles.
public Vector(Collection<? extends T> c)
Creates a vector that contains all the elements of the collection c , in the same order. In other
words, the elements have the same index in the vector created as they do in c . This is not quite a
true copy constructor because it does not preserve capacity. The capacity of the created vector
will be c.size() , not c.capacity .
The vector created is only a shallow copy of the collection argument. The vector created contains
references to the elements in c (not references to clones of the elements in c ).
public Vector(int initialCapacity, int capacityIncrement)
Constructs an empty vector with the specified initial capacity and capacity increment. When the
vector needs to grow, it will add room for capacityIncrement more items.
(ArrayList<T> does not have a corresponding constructor.)
ARRAYLIKE METHODS FOR BOTH ArrayList<T> AND Vector<T>
public T set ( int index, T newElement)
Sets the element at the specified index to newElement . The element previously at that position
is returned. If you draw an analogy to an array a , this is analogous to setting a[index] to the
value newElement . The index must be a value greater than or equal to zero and strictly less
than the current size of the list.
public T get( int index)
Returns the element at the specified index. This is analogous to returning a[index] for an array
a . The index must be a value greater than or equal to 0 and less than the current size of the
calling object.
METHODS TO ADD ELEMENTS FOR BOTH ArrayList<T> AND Vector<T>
public boolean add(T newElement)
Adds newElement to the end of the calling object's list and increases its size by one. The capacity
of the calling object is increased if that is required. Returns true if the add was successful. This
method is often used as if it were a void method.
public void add( int index, T newElement)
Inserts newElement as an element in the calling object at the specified index and increases the size
of the calling object by one. Each element in the calling object with an index greater than or equal to
index is shifted upward to have an index that is one greater than the value it had previously.
The index must be a value greater than or equal to zero and less than or equal to the size of the
calling object (before this addition).
Note that you can use this method to add an element after the last current element. The capacity
of the calling object is increased if that is required.
 
Search WWH ::




Custom Search