{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CarrCh05v2 - C H A P T E R 5 List Implementations That Use...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
97 C H A P T E R 5 List Implementations That Use Arrays C ONTENTS Using a Fixed-Size Array to Implement the ADT List An Analogy The Java Implementation Using Dynamic Array Expansion to Implement the ADT List Expanding an Array A New Implementation of a List Using a Vector to Implement the ADT List A Summary of Methods in the Class Vector The Pros and Cons of Using an Array to Implement the ADT List Java Class Library The Class ArrayList The Interface Serializable P REREQUISITES Chapter 3 Designing Classes Chapter 4 Lists O BJECTIVES After studying this chapter, you should be able to Implement the ADT list by using a fixed-size array, an array that you expand dynamically, or an instance of Vector Discuss the advantages and disadvantages of the three implementations presented Y ou have seen several examples of how to use the ADT list in a program. This chapter presents three different ways that you can implement a list in Java. Each of these ways involves an array. You will see a completely different implementation in the next chapter.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
98 CHAPTER 5 List Implementations That Use Arrays We begin by using an ordinary Java array to represent the entries in a list. With this implemen- tation, your list could become full, just as a handwritten list can fill a page. We then offer two other implementations that do not suffer from this problem. When you use all of the space in an array, Java enables you to move the data to a larger array. The effect is to have an array that apparently expands to meet your needs. Alternately, you can use an instance of the Java class Vector to repre- sent the list entries. The result is like using an array that can expand, since the underlying imple- mentation of Vector uses such an array. But this list implementation is simpler to write than one that uses an array, because Vector does the work for you. Using a Fixed-Size Array to Implement the ADT List We begin by using an analogy to describe how a fixed-size array could represent a list. In doing so, we show how the add and remove methods would work. Subsequently, we present a corresponding Java implementation for the list. An Analogy 5.1 Imagine a classroom—call it room A—containing 40 desks in a fixed position. If a course is restricted to 30 students, 10 desks are idle and wasted. If we lift the enrollment restriction, we can accommodate only 10 more students, even if 20 more want to take the course. An array is like this classroom, and each desk is like one array location. Suppose that we num- ber the 40 desks in the room sequentially, beginning with zero, as Figure 5-1 illustrates. Although desks are arranged in rows in typical classrooms, we will ignore this detail and treat the desks as a one-dimensional array.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}