ITEC200 Week04 Lists and the Collection Interface.
-
Upload
alexandro-radway -
Category
Documents
-
view
216 -
download
0
Transcript of ITEC200 Week04 Lists and the Collection Interface.
ITEC200 Week04
Lists and the Collection Interface
www.ics.mq.edu.au/ppdp 2
Learning Objectives Week04Lists and the Collection Interface (Ch4)
Students can• Explain the difference between various types of List structures
(ArrayList, LinkedList) and their representation in the Java• Utilise List operations to maintain lists of information • Analyse implementations (both the approach to programming and the
corresponding efficiency) of various kinds of Lists (eg, array based, single-, double-, and circular linked)
• Program augmentations to list implementations • Explain the mechanics of the Iterator interface, analyse implementations
of iterators and augment them according to specifications provided• Describe the features of the Java Collection framework in relation to
data structures• Apply Java 5.0 generics to the implementation of Lists
www.ics.mq.edu.au/ppdp 3
The ArrayList Class
• An ArrayList is an Abstract Data Type for storing lists of data.
• Improvement over an array object
• Implements the List interface
www.ics.mq.edu.au/ppdp 4
The List Interface
www.ics.mq.edu.au/ppdp 5
Generic Collections
• Language feature introduced in Java 5.0 called generic collections (or generics)
• Generics allow you to define a collection that contains references to objects of a specific type
• List<String> myList = new ArrayList<String>();
specifies that myList is a List of String where String is a type parameter
• Only references to objects of type String can be stored in myList, and all items retrieved would be of type String.
• A type parameter is analogous to a method parameter.
www.ics.mq.edu.au/ppdp 6
Creating a Generic Collection
www.ics.mq.edu.au/ppdp 7
Specification of the ArrayList Class
www.ics.mq.edu.au/ppdp 8
Implementation of an ArrayList Class
• KWArrayList: simple implementation of a ArrayList class– Physical size of array indicated by data field capacity
– Number of data items indicated by the data field size
www.ics.mq.edu.au/ppdp 9
Implementation of an ArrayList Class (continued)
www.ics.mq.edu.au/ppdp 10
The Vector Class
• Initial release of Java API contained the Vector class which has similar functionality to the ArrayList– Both contain the same methods
• New applications should use ArrayList rather than Vector• Stack is a subclass of Vector
www.ics.mq.edu.au/ppdp 11
Single-Linked Lists and Double-Linked Lists
• The ArrayList: add and remove methods operate in linear time because they require a loop to shift elements in the underlying array
• A Linked list consists of a set of ‘nodes’, each of which contains its data and a reference to the next node
• 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
www.ics.mq.edu.au/ppdp 12
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
www.ics.mq.edu.au/ppdp 13
Single-Linked Lists
www.ics.mq.edu.au/ppdp 14
Double-Linked Lists
• Limitations of a single-linked list include:– Insertion at the front of the list is O(1).
– Insertion at other positions is O(n) where n is the size of the list.
– 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)
www.ics.mq.edu.au/ppdp 15
Double-Linked Lists (continued)
www.ics.mq.edu.au/ppdp 16
Circular Lists
• Circular-linked list: link the last node of a double-linked list to the first node and the first to the last
www.ics.mq.edu.au/ppdp 17
The LinkedList<E> Class
• Part of the Java API• Implements List<E> interface using a double-linked list
www.ics.mq.edu.au/ppdp 18
The Iterator<E> Interface
• The List interface declares the method iterator, which returns an Iterator object that will iterate over the elements of that list
• The interface Iterator is defined as part of API package java.util
• An Iterator does not refer to or point to a particular node at any given time but points between nodes
www.ics.mq.edu.au/ppdp 19
The Iterator<E> Interface (continued)
www.ics.mq.edu.au/ppdp 20
The ListIterator<E> Interface
• Iterator has limitations• ListIterator<E> is an extension of the Iterator<E>
interface for overcoming these limitations• Iterator should be thought of as being positioned
between elements of the linked list
www.ics.mq.edu.au/ppdp 21
The ListIterator<E> Interface (continued)
www.ics.mq.edu.au/ppdp 22
The Enhanced for Statement
www.ics.mq.edu.au/ppdp 23
The Iterable Interface
• This interface requires only that a class that implements it provide an iterator method
• The Collection interface extends the Iterable interface, so all classes that implement the List interface (a subinterface of Collection) must provide an iterator method
www.ics.mq.edu.au/ppdp 24
Implementation of a Double-Linked List
www.ics.mq.edu.au/ppdp 25
Implementation of a Double-Linked List (continued)
www.ics.mq.edu.au/ppdp 26
Application of the LinkedList Class
• Case study that uses the Java LinkedList class to solve a common problem: maintaining an ordered list
www.ics.mq.edu.au/ppdp 27
Application of the LinkedList Class (continued)
www.ics.mq.edu.au/ppdp 28
The Collection interface
• 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
www.ics.mq.edu.au/ppdp 29
The Collection Hierarchy
www.ics.mq.edu.au/ppdp 30
Where to from here…
• Work through Chapter 4 of the Koffman & Wolfgang Text
• Conceptual Questions and Practical Exercises• Submit all preliminary work• Be prompt for your online class
www.ics.mq.edu.au/ppdp 31
Acknowledgements
These slides were based upon the Objects, Abstraction, Data Structures and Design using Java Version 5.0 Chapter 4 PowerPoint presentation
by Elliot B. Koffman and Paul A. T. Wolfgang