8.10. Vectors

Arrays are extremely useful, but they are limited by the fact that they cannot grow or change in size over time. To address this limitation, Java provides a “stretchable” array class: java.util.Vector. It is used for many of the same purposes as linked lists because you can insert or remove elements at any location. Arrays are used for the underlying implementation of Vector. Therefore, it only takes constant time to access a specified location, but takes time proportional to the number of elements contained to insert elements at the beginning or in the middle of the Vector.

Following is a summary of the Vector methods; see Section 8.11 for an example. Notice that the insertion and retrieval methods return elements of type Object ...

Get Core Web Programming, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.