itec200 week04 lists and the collection interface

31
ITEC200 Week04 Lists and the Collection Interface

Upload: alexandro-radway

Post on 30-Mar-2015

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: ITEC200 Week04 Lists and the Collection Interface

ITEC200 Week04

Lists and the Collection Interface

Page 2: 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

Page 3: ITEC200 Week04 Lists and the Collection Interface

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

Page 4: ITEC200 Week04 Lists and the Collection Interface

www.ics.mq.edu.au/ppdp 4

The List Interface

Page 5: ITEC200 Week04 Lists and the Collection 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.

Page 6: ITEC200 Week04 Lists and the Collection Interface

www.ics.mq.edu.au/ppdp 6

Creating a Generic Collection

Page 7: ITEC200 Week04 Lists and the Collection Interface

www.ics.mq.edu.au/ppdp 7

Specification of the ArrayList Class

Page 8: ITEC200 Week04 Lists and the Collection Interface

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

Page 9: ITEC200 Week04 Lists and the Collection Interface

www.ics.mq.edu.au/ppdp 9

Implementation of an ArrayList Class (continued)

Page 10: ITEC200 Week04 Lists and the Collection Interface

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

Page 11: ITEC200 Week04 Lists and the Collection Interface

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

Page 12: ITEC200 Week04 Lists and the Collection Interface

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

Page 13: ITEC200 Week04 Lists and the Collection Interface

www.ics.mq.edu.au/ppdp 13

Single-Linked Lists

Page 14: ITEC200 Week04 Lists and the Collection Interface

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)

Page 15: ITEC200 Week04 Lists and the Collection Interface

www.ics.mq.edu.au/ppdp 15

Double-Linked Lists (continued)

Page 16: ITEC200 Week04 Lists and the Collection Interface

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

Page 17: ITEC200 Week04 Lists and the Collection Interface

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

Page 18: ITEC200 Week04 Lists and the Collection Interface

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

Page 19: ITEC200 Week04 Lists and the Collection Interface

www.ics.mq.edu.au/ppdp 19

The Iterator<E> Interface (continued)

Page 20: ITEC200 Week04 Lists and the Collection Interface

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

Page 21: ITEC200 Week04 Lists and the Collection Interface

www.ics.mq.edu.au/ppdp 21

The ListIterator<E> Interface (continued)

Page 22: ITEC200 Week04 Lists and the Collection Interface

www.ics.mq.edu.au/ppdp 22

The Enhanced for Statement

Page 23: ITEC200 Week04 Lists and the Collection Interface

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

Page 24: ITEC200 Week04 Lists and the Collection Interface

www.ics.mq.edu.au/ppdp 24

Implementation of a Double-Linked List

Page 25: ITEC200 Week04 Lists and the Collection Interface

www.ics.mq.edu.au/ppdp 25

Implementation of a Double-Linked List (continued)

Page 26: ITEC200 Week04 Lists and the Collection Interface

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

Page 27: ITEC200 Week04 Lists and the Collection Interface

www.ics.mq.edu.au/ppdp 27

Application of the LinkedList Class (continued)

Page 28: ITEC200 Week04 Lists and the Collection Interface

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

Page 29: ITEC200 Week04 Lists and the Collection Interface

www.ics.mq.edu.au/ppdp 29

The Collection Hierarchy

Page 30: ITEC200 Week04 Lists and the Collection Interface

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

Page 31: ITEC200 Week04 Lists and the Collection Interface

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