1 / 20

Lists

Lists. Chapter 4. Chapter Contents. Specifications for the ADT List Redefining the Specifications Using the ADT List Java Class Library: The Interface List Using a List Is Like Using a Vending Machine. Specifications for the ADT List. A list provides a way to organize data.

gay-sears
Télécharger la présentation

Lists

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lists Chapter 4

  2. Chapter Contents • Specifications for the ADT List • Redefining the Specifications • Using the ADT List • Java Class Library: The Interface List • Using a List Is Like Using a Vending Machine

  3. Specifications for the ADT List • A list provides a way to organize data lists are “sequential” Fig. 4-1 A to-do list. not accidental http://en.wikipedia.org/wiki/Abstract_data_type

  4. Specifications for the ADT List • Operations on lists • Add new entry – at end, or anywhere • Remove an item • Remove all items • Replace an entry • Look at any entry • Look for an entry of a specific value • Count how many entries • Check if list is empty, full • Display all the entries

  5. Specifications for the ADT List • To specify an ADT list • Describe its data • Specify the operations (fewer than prev slide) • ADT list must be considered in general without regard to implementation. • Not necessarily a list of strings • Note specification, page 83, 84

  6. Example importance of toString() in verifying spec Fig. 4-2 The effect of ADT list operations on an initially empty list

  7. Potential Problem Operations • add, remove, replace, getEntry work OK when valid position given • remove, replace and getEntry not meaningful on empty lists • A list could become full, what happens to add?

  8. Possible Solutions impose preconditions • Assume the invalid situations will not occur • Ignore the invalid situations • Make reasonable assumptions, act in predictable way • Return boolean value indicating success or failure of the operation • Throw an exception no exceptions robust implementation Step 1: Identify unusual situations Step 2: Specify how your ADT will behave.

  9. Listing 4-1 (1): /** An interface for the ADT list. * Entries in the list have positions that begin with 1. */ public interface ListInterface { /** Task: Adds a new entry to the end of the list. * @param newEntry the object to be added as a new entry * @return true if the addition is successful, or false if not */ public boolean add(Object newEntry); /** Task: Adds a new entry at a specified position within * the list. Entries originally at and above the specified * position are at the next higher position within the list. * The listâs size is increased by 1. * @param newPosition an integer that specifies the desired * position of the new entry; newPosition >= 1 * and newPosition <= getLength()+1 * @param newEntry the object to be added as a new entry * @return true if the addition is successful, or false if not */ public boolean add(int newPosition, Object newEntry);

  10. Listing 4-1 (2): /** Task: Removes the entry at a given position from the list. * Entries originally at positions higher than the given * position are at the next lower position within the list, * and the listâs size is decreased by 1. * @param givenPosition an integer that indicates the position of * the entry to be removed; givenPosition >= 1 * and givenPosition <= getLength() * @return either the entry at position givenPosition, if the removal * was successful, or null */ public Object remove(int givenPosition); /** Task: Removes all entries from the list. */ public void clear();

  11. Listing 4-1 (3): /** Task: Replaces the entry at a given position in the list. * @param givenPosition an integer that indicates the position of the * entry to be replaced; givenPosition >= 1 * and givenPosition <= getLength() * @param newEntry the object that will replace the entry at the * position givenPosition * @return true if the replacement occurs, or false if either the * list was empty or givenPosition is invalid */ public boolean replace(int givenPosition, Object newEntry); /** Task: Retrieves the entry at a given position in the list. * @param givenPosition an integer that indicates the position of * the desired entry; givenPosition >= 1 * and givenPosition <= getLength() * @return a reference to the indicated list entry, if found, * otherwise returns null */ public Object getEntry(int givenPosition);

  12. Listing 4-1 (4): /** Task: Determines whether the list contains a given entry. * @param anEntry the object that is the desired entry * @return true if the list contains anEntry, or false if not */ public boolean contains(Object anEntry); /** Task: Gets the length of the list. * @return the integer number of entries currently in the list */ public int getLength(); /** Task: Determines whether the list is empty. * @return true if the list is empty, or false if not */ public boolean isEmpty(); /** Task: Determines whether the list is full. * @return true if the list is full, or false if not */ public boolean isFull(); /** Task: Displays all entries that are in the list, one per * line, in the order in which they occur in the list. */ public void display(); } // end ListInterface better to use toString()

  13. Redefining Specifications • A first draft of an ADT specifications may ignore potential problems • Simplifies the first draft • Concentrate on details after major portions of specifications written • Makes the specifications complete • After writing specifications, implementations • Write Java statements to use the ADT • Checks understanding, suitability of the specifications test code

  14. Using the ADT List Fig. 4-3 A list of numbers that identify runners in the order in which they finish a race

  15. Using the ADT List public class ListClient { public static void main(String[] args) { testList(); } // end main public static void testList() { ListInterface runnerList = new AList(); // has only methods // in ListInterface runnerList.add("16"); // winner runnerList.add(" 4"); // second place runnerList.add("33"); // third place runnerList.add("27"); // fourth place runnerList.display(); } // end testList } // end ListClient can only call ListInterface methods AList implements interface ListInterface pg. 88, 89 of text.

  16. Java Class Library: The Interface List • The standard package contains a list interface – called List • Methods provided public boolean add(Object newEntry) public void add(int index, Object newEntry) public Object remove(int index) public void clear() public Object set(int index, Object anEntry) // like replace public Object get(int index) // like getEntry public boolean contains(Object anEntry) public int size() // like getLength public boolean isEmpty() Exercise: Find exceptions thrown by these methods add: UnsupportedOperationException ClassCastException NullPointerException IllegalArgumentException

  17. What if you don’t Implement all Methods: public interface MyInterface<T> { public void add(T t) throws UnsupportedOperationException ; . . . } public class MyClass<T> implements MyInterface<T> { public void add(T t) { throw new UnsupportedOperationException(“Not implemented”); } . . . }

  18. A List is Like a Vending Machine all about the user interface: - only do certain things - you need to understand these things - can’t get inside - don’t need to get inside - replacing the insides won’t affect how you use the machine Fig 4-4 A vending machine.

  19. A List is Like a Vending Machine Observations about vending machines • Can perform only tasks shown on interface • Must understand the tasks • Cannot see inside the machine • Can use the machine even though don’t know what happens inside • If inside of machine replaced with new improved version • Interface remains unchanged • Customer uses machine in same way as before

  20. A List is Like a Vending Machine Observations about clients and List ADT • Client can perform only operations from the ADT List • Client must adhere to specifications • Client cannot access data without an ADT operation • Client can use the list – even though unable to access entries directly • If implementation is changed, client still uses list in same way as before

More Related