Java Reference
In-Depth Information
ception
is thrown. If the first argument is greater than the second argument,
IllegalArgumentException
is thrown.
You have lots of ways of using the
subList()
method in conjunction with other methods, for example:
ListIterator<String> listIter = names.subList(5, 15).listIterator(2);
The call to
subList()
returns a
List<String>
object that encapsulates the elements from
names
at index
positions 5 to 14, inclusive. You then call the
listIterator()
method for this object, which returns a list
iterator of type
ListIterator<String>
for elements in the
List<String>
collection from index position
2 to the end. This corresponds to elements 7 to 14, inclusive, from the original
names
vector. You can use
this iterator to roam backward and forward through elements 7 to 14 from the
names
vector to your heart's
content.
Extracting All the Elements from a Vector
A
Vector<>
object provides you with tremendous flexibility in use, particularly with the capability to auto-
matically adjust its capacity. Of course, the flexibility you get comes at a price. There is always some over-
head involved when you're retrieving elements. For this reason, there might be times when you want to
retrieve the elements contained in a
Vector<>
object as a regular array. The
toArray()
method that both
array collections implement does this for you. You typically use the method like this:
String[] data = names.toArray(new String[names.size()]);
The argument to the
toArray()
method must be an array of the same type or a supertype of the type
of elements in the vector. If it isn't, an
ArrayStoreException
is thrown. If the argument is
null
, then a
NullPointerException
is thrown. If the array argument is not large enough to accommodate all the ele-
ments in the vector, then a new array is created, and a reference to that is returned. The
toArray()
method
here returns an array of type
String[]
containing all the elements from
names
in the correct sequence.
It's worth noting that the
java.util.Arrays
class you first saw in Chapter 3 defines a static parameter-
ized method,
asList()
, that converts an array of a given type,
T
, into a
List<T>
collection. The argument
is the array of type
T
that you want to convert, and the reference returned is of type
List<T>
. For example:
String[] people = { "Brian", "Beryl", "Belinda", "Barry", "Bill", "Barbara" };
List<String> nameList = java.util.Arrays.asList(people);
Note that the
List<>
object that is returned does not have storage independent of the array. The
List<>
object is backed by the array you pass as the argument. From the interface hierarchy that you saw earlier
you know that a
List<String>
reference is also a
Collection<String>
reference. You can therefore pass
it as an argument to a
Vector<String>
constructor. For example:
Vector<String> names = new Vector<>( java.util.Arrays.asList(people));
Here you are calling the constructor that accepts an argument of type
Collection<>
. You thus have a
way to create a
Vector<>
object containing the elements from a predefined array. Of course, the type of
elements in the array must be consistent with the type argument for the vector you are creating.
Removing Objects