Java Reference
In-Depth Information
Display 16.7. A table of methods for the
Vector<T>
class is also given in Display 16.7.
A more complete list of the methods in these classes is given in Appendix 5. If you want
to define your own class that implements the
List<T>
interface, you would probably
be better off using either the
ArrayList<T>
or the
Vector<T>
class rather than the
AbstractList<T>
class as a base class.
The abstract class
AbstractSequentialList<T>
is derived from the
AbstractList<T>
class. Although it does override some methods inherited from
the class
AbstractList<T>
, it adds no completely new methods. The point of the
AbstractSequentialList<T>
class is to provide for efficient implementation of
sequentially moving through the list at the expense of having inefficient implementation
of random access to elements (that is, inefficient implementation of the
get
method). The
LinkedList<T>
class is a concrete derived class of the abstract class
AbstractSequentialList<T>
. (The implementation of the
LinkedList<T
> class is
similar to that of the linked list classes we discussed in Chapter 15.) If you need a
List<T>
with efficient random access to elements (that is, efficient implementation of
Abstract
Sequential
List<T>
LinkedList<T>
Display 16.7
Methods in the Classes
ArrayList<T>
and
Vector<T>
(part 1 of 4)
The
ArrayList<T>
and
Vector<T>
classes and the
Iterator<T>
and
ListIterator<T>
interfaces are in the
java.util
package.
All the exception classes mentioned are unchecked exceptions, which means they are not
required to be caught in a
catch
block or declared in a
throws
clause. (If you have not yet
studied exceptions, you can consider the exceptions to be run-time error messages.)
NoSuchElementException
is in the
java.util
package, which requires an import statement
if your code mentions the
NoSuchElementException
class. All the other exception classes
mentioned are in the package
java.lang
and so do not require any import statement.
CONSTRUCTORS
public
ArrayList(
int
initialCapacity)
Creates an empty
ArrayList<T>
with the specified initial capacity. When the
ArrayList<T>
needs to increase its capacity, the capacity doubles.
public
ArrayList()
Creates an empty
ArrayList<T>
with an initial capacity of
10
. When the
ArrayList<T>
needs
to increase its capacity, the capacity doubles.
public
ArrayList(Collection<?
extends
T> c)
Creates an
ArrayList<T>
that contains all the elements of the collection
c
, in the same order.
In other words, the elements have the same index in the
ArrayList<T>
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 list will be
c.size()
, not
c.capacity
.
The
ArrayList<T>
created is only a shallow copy of the collection argument. The
ArrayList<T>
created contains references to the elements in
c
(not references to clones of the elements in
c
).
public
Vector(
int
initialCapacity)
Creates an empty vector with the specified initial capacity. When the vector needs to increase its
capacity, the capacity doubles.
(continued)