Java Reference
In-Depth Information
It is important to keep in mind that any collection class object that implements the
Collection
interface can be referenced using a variable of type
Collection
. This means that any of the list or set
collections can be referenced in this way; only the map class types are excluded (but not entirely, as you
can obtain a list from a map and the classes implementing a map can provide a view of the values stored
as a
Collection
reference). You will see that using a parameter of type
Collection
is a standard
way of passing a list or set to a method.
These interfaces involve quite a number of methods, so rather than go through them in the abstract, let's
see them at work in the context of specific classes. We will look at the
Vector
class first since it is close
to the notion of an array that you are already familiar with.
Using Vectors
The
Vector
class defines a collection of elements of type
Object
that works rather like an array, but
with the additional feature that it can grow itself automatically when you need more capacity. It
implements the
List
interface so it can be used as a list. Because it stores elements of type
Object
,
and
Object
is a superclass of every object, you can store any type of object in a
Vector
. This also
means that potentially you can use a single
Vector
object to store objects that are instances of a variety
of different classes. This is another advantage the
Vector
class has over arrays, but the circumstances
where this is desirable are relatively rare.
This ability to store diverse objects has a downside. It implies that it's very easy for you to store objects
in a
Vector
by mistake. You can set up a
Vector
in which you plan to store a particular kind of
object, but there's nothing to prevent the storage of some other kind of object in the
Vector
, or to
signal that this may cause problems. If you need to protect against this kind of error, you must program
for it yourself. This isn't terribly difficult. As you'll see later in this chapter, all you need to do is package
your
Vector
as a private member of a class that you define, and then supply methods to store objects
in the
Vector
that will only accept the type that you want.
Like arrays, vectors only hold object references, not actual objects. To keep things
simple we refer to a
Vector
as holding objects. We'll make the distinction only when
it's important, but you should keep in mind that all the collection classes you're about
to encounter hold object references.
Creating a Vector
There are four constructors for a
Vector
. The default constructor creates an empty
Vector
object with
the capacity to store up to a default number of objects, and the
Vector
object will increase in size each
time you add an element when the
Vector
is full. The default capacity of a
Vector
object is ten
objects, and the
Vector
object will double in size when you add an object when it is full. For example:
Vector transactions = new Vector(); // Create an empty Vector