Download presentation
Presentation is loading. Please wait.
Published byAlexandro Radway Modified over 10 years ago
1
ITEC200 Week04 Lists and the Collection Interface
2
www.ics.mq.edu.au/ppdp 2 Learning Objectives Week04 Lists 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
3
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
4
www.ics.mq.edu.au/ppdp 4 The List Interface
5
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 myList = new ArrayList (); 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.
6
www.ics.mq.edu.au/ppdp 6 Creating a Generic Collection
7
www.ics.mq.edu.au/ppdp 7 Specification of the ArrayList Class
8
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
9
www.ics.mq.edu.au/ppdp 9 Implementation of an ArrayList Class (continued)
10
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
11
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
12
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
13
www.ics.mq.edu.au/ppdp 13 Single-Linked Lists
14
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)
15
www.ics.mq.edu.au/ppdp 15 Double-Linked Lists (continued)
16
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
17
www.ics.mq.edu.au/ppdp 17 The LinkedList Class Part of the Java API Implements List interface using a double-linked list
18
www.ics.mq.edu.au/ppdp 18 The Iterator 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
19
www.ics.mq.edu.au/ppdp 19 The Iterator Interface (continued)
20
www.ics.mq.edu.au/ppdp 20 The ListIterator Interface Iterator has limitations ListIterator is an extension of the Iterator interface for overcoming these limitations Iterator should be thought of as being positioned between elements of the linked list
21
www.ics.mq.edu.au/ppdp 21 The ListIterator Interface (continued)
22
www.ics.mq.edu.au/ppdp 22 The Enhanced for Statement
23
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
24
www.ics.mq.edu.au/ppdp 24 Implementation of a Double- Linked List
25
www.ics.mq.edu.au/ppdp 25 Implementation of a Double- Linked List (continued)
26
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
27
www.ics.mq.edu.au/ppdp 27 Application of the LinkedList Class (continued)
28
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
29
www.ics.mq.edu.au/ppdp 29 The Collection Hierarchy
30
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
31
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.