1 / 14

The List Interface

The List Interface. Contents. General description of the List Interface Common methods implemented in this interface A List can be declared in an application and implemented by any class that implements the interface Iterators. Interface Collection. Interface List.

Télécharger la présentation

The List Interface

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. The List Interface Contents • General description of the List Interface • Common methods implemented in this interface • A List can be declared in an application and implemented by any class that implements the interface • Iterators

  2. Interface Collection Interface List The Collection Interface interface List extends Collection

  3. Interfaces Collection and List The Collection interface contains the following methods + those inherited from object Collection<String> vector = new ArrayList<String>( ) add(String arg0) boolean addAll(Collection<? Extends String> arg) boolean clear( ) void contains(Object arg0) boolean isEmpty( ) boolean iterator( ) Iterator<String> remove(Object arg0) boolean toArray( ) Object[ ] toArray(T [ ] arg0) T[ ]

  4. Interfaces List and Collection Interface List extends Collection and adds these new methods: List<String> vector = new ArrayList<String>( ); add(int index, String arg0) boolean get(int index) String indexOf(Object arg0) int remove(int index) String set(int index, String arg0) String lastIndexOf(Object arg0) int listIterator( ) ListIterator<String> listIterator(int index) ListIterator<String> subList(int fromIndex, int toIndex) List<String> List methods make use of the fact that lists are ordered (indexed) collections

  5. The List Interface A list is a sequence of objects. In a sequence every object (except the first and last) has a predecessor and a successor, and every object occupies a unique position in the sequence. • An object in a sequence can be referenced by its position. • Objects in a sequence (except the first and last) have a unique predecessor and successor. A List will have the following operations performed upon it. • Insert an object at a specified index • Append an object at the end of the list • Remove an object at a specified index • Remove an object identified by name from the list • Retrieve an object at a specified index • Overwrite the contents at a specified index with a new object • Find the index of the first occurrence of an object in the list • Traverse the list to “visit” each indexed object in the list

  6. LinkedList implementation List Client Application List Methods ArrayList implementation List Interface On the previous slide we specified (most of) the functions that a List object should provide, but we did not indicate how such a class should be implemented. A List may be implemented either as an array or a linked list. The List provides a common interface that hides the implementation details from the user. OR Interface Implemented by

  7. Append Object to receiver (add to the end of the sequence) Insert Object at indicated index (adjust the rest of the list) Returns TRUE if the receiver contains the Object Returns a copy of the Object located at the index Returns the index of the first occurrence of Object – returns length of list if the Object is not present Remove Object at indicated index (adjust the rest of the list) Remove the first occurrence of Object (adjust rest of the list) Overwrite contents of list at position of index with Object Returns true if the list is empty Returns the length of the list List Interface The List is NOT a class in Java, but an Interface that provides a standard syntax for a client to use to send messages to the container and for the supplier to use in a class that implements the Interface. Some List methods Method Description add(Object) add(index, Object)** contains(Object) get(index)** indexOf(Object) remove(index)** remove(Object) set(index, Object)** isEmpty( ) size( ) listIterator( ) Returns a ListIterator to traverse the referenced container

  8. boolean return indicates whether the operation completed successfully Constructs an Iterator and sets it to locate the first item. List Interface publicinterface List { boolean add(int index, Object obj); boolean add(Object obj); Object remove(int index); boolean remove(Object obj); Object set(int index, Object obj); Object get(int index); boolean contains(Object obj); int indexOf(Object obj); int size( ); boolean isEmpty( ); ListIterator listIterator( ); void clear( ); ……. }

  9. The same signature is used for method calls in each of the Lists ListInterface The Application can declare an instance of a List object and assign either an ArrayList or a LinkedList to it. publicstaticvoid main(String [ ] args) { List L1; List L2; L1 = new ArrayList( ); L2 = new LinkedList( ); for (int i = 0; i < 10 ; i++) { L1.add(i, new Integer(i)); L2.add(i, new Integer(i)); } //etc. }

  10. ListInterface A general method can be written to apply to any container that implements the List interface. public void removeAll( List theContainer, Object theTarget) { …} /** this method removes all occurrences of theTarget from ANY container that implements the List interface */

  11. List Interface Iterators In object-oriented programming one distinguishes between the container object that holds data and an Iterator object that traverses the container and visits the data items. To use an Iterator perform the following steps: • Obtain an iterator from the Collection object (List) by using a method (in the interface) iterator( ). This method call returns an iterator object and sets it to read the first element in the list. • Get the next object in sequence with method next( ). • See if there are any more elements in the sequence with a call to hasNext( ). • You may remove the last item returned by the iterator from the list with a call to remove( )

  12. The method receives an iterator object as its argument withno distinction made about what collection produced it. Obtain an iterator from the ArrayList and pass it to the Printer List Interface Iterators With an iterator one can write generic methods to handle any collection publicclass Printer { staticvoid printAll (Iterator e) { while (e.hasNext( ) ) System.out.print (e.next( ) + “ ” ); System.out.println( ); } publicclass FrogPond { publicstatic void main(String [ ] args) { ArrayList v = new ArrayList( ); for (int i = 0; i < args[0]; i++) v.add(new Frog(i)); Printer.printAll(v.iterator( )); } }

  13. Produces a ListIterator set to visit the element at index 5 List Interface ListIterator A ListIterator is produced by any Container implementing the List interface. It has an additional constructor that takes an index value as a parameter and returns an iterator set to begin visiting the element at this location on the first call to next( ). LinkedList myL = new LinkedList( ); //fill the list with objects ListIterator itr = myL.ListIterator(5); A client program can maintain multiple iterators to facilitate searches and swaps inside of a list.

  14. List Interface ListIterator methods In addition to methods next( ), hasNext( ), and remove( ) that are inherited from interface Iterator, ListIterator provides these additional methods public int nextIndex( ); //returns index of the element that would be called by next( ) public int previousIndex ( ); public boolean hasPrevious( ); public Object previous( ) throws NoSuchElementException; public void add (Object o) throws UnsupportedOperationException, ClassCastException, IllegalArgumentException; //inserts the object into the list at current index

More Related