Chapter 4: Array Implementations of Collection ADTs
General Approach to Implementing ADTs with Arrays
Definition
A standardized three-step process for creating and using an Abstract Data Type (ADT): specification, implementation (via interface and class), and usage in client programs.
Causes
Need to separate design from implementation to support abstraction, encapsulation, and code reuse.
Goals / Objectives
Enable modular, maintainable, and reusable code
Hide implementation details from users of the ADT
Importance
Forms the foundation for implementing collection ADTs like list, stack, and queue
Supports separation of concerns and information hiding
Procedures
Step 1: Write the ADT specification in natural language—describe data characteristics and operations without implementation details
Step 2a: Translate the specification into a Java interface containing method declarations
Step 2b: Write a Java class that:
Implements the interface
Defines data representation (e.g., array + size tracker)
Implements all operations (core and utility methods)
Step 3: Use the ADT in a client program or application
Advantages & Disadvantages
Advantages:
Promotes code reuse and maintainability
Allows implementation changes without affecting client code
Disadvantages:
Not specified in notes
Impact / Effect
Client programs depend only on the interface, not the implementation
Bugs or improvements are localized to the implementing class
Examples
Used as the framework for implementing List, Stack, and Queue ADTs in this chapter
List ADT – Array Implementation
Definition
A linear collection ADT that stores entries in sequence and allows operations like add, remove, replace, and access by position, implemented using an array.
Causes
Need for an ordered, index-based collection that supports flexible insertion and retrieval
Goals / Objectives
Provide a reusable, encapsulated list structure
Support operations at arbitrary positions (not just ends)
Importance
Lists are fundamental in many applications (e.g., to-do lists, playlists)
Array implementation offers fast random access
Procedures
Data fields:
T[] array: stores list entries (generic type)
int numberOfEntries: tracks current size
Key methods:
add(newEntry): appends to end
add(position, newEntry): inserts at given position using makeRoom() to shift elements right
remove(position): removes entry and shifts left using removeGap(), returns null on invalid input
getEntry(position), replace(position, newEntry), contains(entry), etc.
Dynamic expansion:
When array is full, doubleArray() creates a new larger array and copies contents
isArrayFull() checks capacity; isFull() always returns false to support expansion
Advantages & Disadvantages
Advantages:
Fast retrieval by index (O(1))
Efficient appending at end (O(1) amortized with expansion)
Disadvantages:
Insertion/removal in middle requires shifting elements (O(n))
Array expansion requires copying all elements (O(n))
Impact / Effect
Enables flexible list usage but with performance trade-offs for mid-list operations
Dynamic expansion avoids hard size limits but adds occasional overhead
A FIFO (First-In-First-Out) collection ADT where elements are added at the back and removed from the front, implemented using arrays in three ways: fixed front, dynamic front, and circular.
Causes
Need for ordered processing (e.g., task scheduling, request handling)
Goals / Objectives
Support efficient enqueue and dequeue operations
Minimize wasted space and shifting overhead
Importance
Essential for modeling real-world queues (e.g., print jobs, customer service lines)
Procedures
Variant 1: Linear Array with Fixed Front
Data fields: frontIndex = 0 (fixed), backIndex = -1 (empty)
enqueue(): increment backIndex, assign value
dequeue(): return array[0], shift all elements left, decrement backIndex
Variant 2: Linear Array with Dynamic Front
Data fields: frontIndex and backIndex both dynamic