An Unallocated Array

In this lesson, we look at what happens when we declare an array, but forget to allocate space for it.

An early version of the class BagOfStrings

The previous chapter used an array of strings in the definition of the class BagOfStrings. We learned to focus on a core group of methods when beginning the definition of a new class, instead of tackling the entire class at once. Even so, our development of this core group likely would progress in smaller steps than that chapter describes. For example, imagine that we have begun writing the class in a simple manner by defining only two data fields—bag and numberOfStrings— a default constructor, a simplified method add, and a stub for toArray, as the program given below shows.

Get hands-on with 1200+ tech skills courses.