1 / 73

Lists and the Collection Interface Review inheritance & collections

Lists and the Collection Interface Review inheritance & collections. Objectives. Theory: To understand list, linked list To study the difference between single-, double-, and circular linked list data structures Implementation: List interface, ArrayList , LinkedList

zorana
Télécharger la présentation

Lists and the Collection Interface Review inheritance & collections

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 and the Collection InterfaceReview inheritance & collections

  2. Objectives • Theory: • To understand list, linked list • To study the difference between single-, double-, and circular linked list data structures • Implementation: • List interface, ArrayList, LinkedList • Review Java inheritance and collection

  3. Insert in the middle of Linkedlist Before After

  4. Review question ______is a version of a linked list in which nodes can be navigated in the forward direction only. • Doubly Linked List • Singly Linked List • Circular Linked List • All of the above

  5. Review question ______is a version of a linked list in which nodes can be navigated in forward direction only. • Doubly Linked List • Singly Linked List • Circular Linked List • All of the above

  6. The List Interface and ArrayList Class • An array is an indexed structure: can select its elements in arbitrary order using a subscript value • Elements may be accessed in sequence using a loop that increments the subscript • You cannot • Increase or decrease the length • Add an element at a specified position without shifting the other elements to make room • Remove an element at a specified position without shifting other elements to fill in the resulting gap

  7. Implementation: List interface

  8. Implementation: List interface • boolean add(Object o) • void clear() • boolean contains(Object o) • Object get(int index) • intindexOf(Object o) • booleanisEmpty() • Iteratoriterator() • intlastIndexOf(Object o) • ListIteratorlistIterator() • ListIteratorlistIterator(int index) • Object remove(int index) • boolean remove(Object o) • int size() • Object[] toArray()

  9. Implementation: List Interface • Allowed operations on the List interface include: • Finding a specified target • Adding an element to either end • Removing an item from either end • Traversing the list structure without a subscript • An array provides the ability to store primitive-type data whereas the List classes all store references to Objects

  10. Review question A method that does not alter a linked list but simply looks at it to determine whether it’s empty, is referred as ________method • isClear() • isNull() • isEmpty() • isZero()

  11. Review question A method that does not alter a linked list but simply looks at it to determine whether it’s empty, is referred as ________method • isClear() • isNull() • isEmpty() • isZero()

  12. Array-based list implementation.ArrayListClass

  13. Array-based list implementation.ArrayListClass • Simplest class that implements the List interface • Improvement over an array object • Used when a programmer wants to add new elements to the end of a list but still needs the capability to access the elements stored in the list in arbitrary order

  14. ArrayList Class

  15. Specification of the ArrayList Class

  16. Application of ArrayList • The ArrayList gives you additional capability beyond what an array provides • ArrayList stores items of type Object and can thus store an object of any class • You cannot store values of the primitive types directly but must instead use wrapper classes • When an object is stored in an ArrayList, the programmer must remember the original type

  17. Single-Linked Lists and Double-Linked Lists • ArrayList: add and remove methods operate in linear time because they require a loop to shift elements in the underlying array • Linked list overcomes this by providing ability to add or remove items anywhere in the list in constant time • Each element (node) in a linked list stores information and a link to the next, and optionally previous, node

  18. A List Node

  19. A List Node • A node contains a data item and one or more links • A link is a reference to a node • A node is generally defined inside of another class, making it an inner class • The details of a node should be kept private

  20. Double-Linked Lists • Limitations of a single-linked list include: • Can insert a node only after a referenced node • Can remove a node only if we have a reference to its predecessor node • Can traverse the list only in the forward direction • Above limitations removed by adding a reference in each node to the previous node (double-linked list)

  21. Double-Linked Lists

  22. Double-Linked Lists Chapter 4: Lists and the Collection Interface

  23. Inserting into a Double-Linked List

  24. Removing from a Double-Linked List

  25. Circular Lists • Circular-linked list: link the last node of a double-linked list to the first node and the first to the last • Advantage: can traverse in forward or reverse direction even after you have passed the last or first node • Can visit all the list elements from any starting point • Can never fall off the end of a list • Disadvantage: infinite loop!

  26. Circular Lists Continued

  27. LinkedListClass • Part of the Java API • Implements the List interface using a double-linked list

  28. The Iterator Interface • The interface Iterator is defined as part of API package java.util • The List interface declares the method iterator, which returns an Iterator object that will iterate over the elements of that list • An Iterator does not refer to or point to a particular object at any given time

  29. Iterator Interface Chapter 3: Inheritance and Class Hierarchies

  30. The ListIterator Interface • Iterator limitations • Can only traverse the List in the forward direction • Provides only a remove method • Must advance an iterator using your own loop if starting position is not at the beginning of the list • ListIterator is an extension of the Iterator interface for overcoming the above limitations • Iterator should be thought of as being positioned between elements of the linked list

  31. The ListIterator Interface (continued)

  32. Comparison of Iterator and ListIterator • ListIterator is a subinterface of Iterator; classes that implement ListIterator provide all the capabilities of both • Iterator interface requires fewer methods and can be used to iterate over more general data structures • Iterator is required by the Collection interface, whereas the ListIterator is required only by the List interface

  33. Conversion between a ListIterator and an Index • ListIterator has the methods nextIndex and previousIndex, which return the index values associated with the items that would be returned by a call to the next or previous methods • The LinkedList class has the method listIterator(int index) • Returns a ListIterator whose next call to next will return the item at position index

  34. The Collection Hierarchy • Both the ArrayList and LinkedList represent a collection of objects that can be referenced by means of an index • The Collection interface specifies a subset of the methods specified in the List interface • Collection interface is the root of the collection hierarchy • Two branches: one rooted by the List interface and the other by the Set interface

  35. The Collection Hierarchy (continued)

  36. Common Features of Collections • Collection interface specifies a set of common methods • Fundamental features include: • Collections grow as needed • Collections hold references to objects • Collections have at least two constructors

  37. Inheritance and Class Hierarchies

  38. Introduction to Inheritance and Class Hierarchies • Popularity of OOP is that it enables programmers to reuse previously written code saved as classes • All Java classes are arranged in a hierarchy, starting with Object, which is the superclass of all Java classes • Inheritance in OOP is analogous to inheritance in humans • Inheritance and hierarchical organization allow you to capture the idea that one thing may be a refinement or extension of another

  39. Introduction to Inheritance and Class Hierarchies (continued)

  40. Is-a Versus Has-a Relationships • One misuse of inheritance is confusing the has-a relationship with the is-a relationship • The has-a relationship means that one class has the second class as an attribute • We can combine is-a and has-a relationships • The keyword extends specifies that one class is a subclass of another

  41. A Superclass and a Subclass • Consider two classes: Computer and Laptop • A laptop is a kind of computer and is therefore a subclass of computer

  42. Initializing Data Fields in a Subclass and the No-Parameter Constructor • Private data fields belonging to a base class must be initialized by invoking the base class’s constructor with the appropriate parameters • If the execution of any constructor in a subclass does not invoke a superclass constructor, Java automatically invokes the no-parameter constructor for the superclass • Initializes that part of the object inherited from the superclass before the subclass starts to initialize its part of the object

  43. Protected Visibility for Superclass Data Fields • Private data fields are not accessible to derived classes • Protected visibility allows data fields to be accessed either by the class defining it or any subclass • In general, it is better to use private visibility because subclasses may be written by different programmers and it is always good practice to restrict and control access to the superclass data fields

  44. Method Overriding • If a derived class has a method found within its base class, that method will override the base class’s method • The keyword super can be used to gain access to superclass methods overridden by the base class • A subclass method must have the same return type as the corresponding superclass method

  45. Method Overloading • Method overloading: having multiple methods with the same name but different signatures in a class • Constructors are often overloaded • Example: • MyClass(int inputA, int inputB) • MyClass(float inputA, float inputB)

More Related