Implementing the List ADT in Java: A Comprehensive Guide
This document provides a detailed overview of implementing a List Abstract Data Type (ADT) in Java. It outlines the process of defining operations such as add, remove, get, and clear, without delving into the implementation details initially, highlighting the principle of encapsulation. Key concepts include managing an array for storage, handling resizing, and implementing error checks for operations. The importance of algorithms for operation effectiveness and data structures for storage is emphasized, ensuring a clear understanding of both the programming interface and underlying complexities.
Implementing the List ADT in Java: A Comprehensive Guide
E N D
Presentation Transcript
Implementing the List ADT Computer Science 4 Mr. Gerb Reference: Objective: Understand how to implement an ADT.
Review • We created an ADT called a list • We defined a number of operations on list • We decided the inputs and outputs of those operations • At no time did we discuss how those operations would be implemented. • But we could use the operations perfectly well without knowing. • Encapsulation!!!
How could we implement a list? // Stores a list as an array class MyList implements List { Object[] storage = new Object[10]; int listSize=0; ... //Methods } • Some notes • “implements List” tells Java that List has been defined as an interface, a set of operations • Array initially given a size of 10. We will deal later with the case when this is not enough.
Implementing the clear method //Very simple, list size = 0 public void clear() { listSize=0; }
Implement the size operator public int size() { return listSize; }
Implementing the get operator // This won’t work: public Object get(int N) { return storage[N]; } • What happens if N >= listSize?
A better implementation of get public Object get(int N) } if (N>=listSize || N<0) throw new ArrayIndexOutOfBoundsException(); else return storage[N]; } • Can implement set in a similar way
Implementing remove, move all subsequent elements down public Object remove(int N) } if (N>=listSize || N<0) throw new ArrayIndexOutOfBoundsException(); else { for (int (m=N+1;m<listSize;++m) storage[m-1]=storage[m]; --listSize; } }
Implementing add, array might be smaller than listSize //Double the size of the array if no space public void add(Object obj) { if (storage.length<=listSize) { Object[] s=new Object[storage.length*2]; for (m=0; m<listSize; ++m) s[m]=storage[m]; storage=s; } storage[listSize] = obj; ++listSize; }
Two parts of the implementation • Algorithms • Process by which operations occur • Examples: Moving down items during a remove or doubling the array during add. • Data structures • How data in the ADT is stored • In this case, an array and an integer
Summary • Implement an ADT with a class • In Java, define the ADT as an interface • Use the implements keyword in the class definition. • Two parts of an implementation • Algorithms • Data structures • Implementation deals with many details that the ADT doesn’t need to deal with.