Storing Objects of the Same Class in Vectors

An important decision to make when writing a computer program is where to store data. In the first half of this book, you've found three useful places to keep information: basic data types like int and char, arrays, and String objects.

Any Java class can hold data. One of the most useful is Vector, a data structure that holds objects of the same class.

Vectors are like arrays, which also hold elements of related data, but they can grow or shrink in size at any time.

The Vector class belongs to the java.util package of classes, one of the most useful in the Java 2 class library. An import statement makes it available in your program:

import java.util.Vector;

A vector holds objects that either belong ...

Get SAMS Teach Yourself Programming with Java™ in 24 Hours, FOURTH EDITION now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.