730 likes | 889 Views
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
E N D
Lists and the Collection InterfaceReview 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 • Review Java inheritance and collection
Insert in the middle of Linkedlist Before After
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
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
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
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()
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
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()
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()
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
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
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
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
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)
Double-Linked Lists Chapter 4: Lists and the Collection Interface
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!
LinkedListClass • Part of the Java API • Implements the List interface using a double-linked list
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
Iterator Interface Chapter 3: Inheritance and Class Hierarchies
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
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
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
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
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
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
Introduction to Inheritance and Class Hierarchies (continued)
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
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
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
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
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
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)