Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.

Slides:



Advertisements
Similar presentations
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Advertisements

ITEC200 Week04 Lists and the Collection Interface.
COMP 121 Week 9: AbstractList and ArrayList. Objectives List common operations and properties of Lists as distinct from Collections Extend the AbstractCollection.
Double-Linked Lists and Circular Lists
CHAPTER 4 Queues. Queue  The queue, like the stack, is a widely used data structure  A queue differs from a stack in one important way  A stack is.
COMP 121 Week 11: Linked Lists. Objectives Understand how single-, double-, and circular-linked list data structures are implemented Understand the LinkedList.
Queues Chapter 6. Chapter Objectives  To learn how to represent a waiting line (queue) and how to use the methods in the Queue interface for insertion.
Sets and Maps Chapter 9. Chapter 9: Sets and Maps2 Chapter Objectives To understand the Java Map and Set interfaces and how to use them To learn about.
Chapter 101 Dynamic Data Structures and Generics Chapter 10.
Lists and the Collection Interface Chapter 4 Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Fall 2007CS 2251 Queues Chapter 6. Fall 2007CS 2252 Chapter Objectives To learn how to represent a waiting line (queue) and how to use the methods in.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Sprint 2010CS 2251 Lists and the Collection Interface Chapter 2.
Queues Chapter 6. Chapter 6: Queues2 Chapter Objectives To learn how to represent a waiting line (queue) and how to use the methods in the Queue interface.
Fall 2007CS 2251 Queues Chapter 6. Fall 2007CS 2252 Chapter Objectives To learn how to represent a waiting line (queue) and how to use the methods in.
Chapter 10 2D Arrays Collection Classes. Topics Arrays with more than one dimension Java Collections API ArrayList Map.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
CSC 142 J(part 1) 1 CSC 142 The ArrayList class [Reading: chapter 10]
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Chapter 11 Arrays Continued
111 © 2002, Cisco Systems, Inc. All rights reserved.
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
Lists and the Collection Interface Review inheritance & collections.
GENERICS. Generics  Classes and methods can have a type parameter (actually, more than one).  The type parameter can be any reference (class) type.
Fall 2007CS 2251 Queues Chapter 6. Fall 2007CS 2252 Chapter Objectives To learn how to represent a waiting line (queue) and how to use the methods in.
CSS446 Spring 2014 Nan Wang  Java Collection Framework ◦ LinkedList ◦ Set ◦ Map 2.
Queues Chapter 6. Chapter 6: Queues Chapter Objectives To learn how to represent a waiting line (queue) and how to use the five methods in the Queue interface:
Lists and the Collection Interface Chapter 4. 2 The List Interface and ArrayList Class So far, all we have is an array for storing a collection of elements.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Generics and Collections Course Lecture Slides 19 th July 2010 “Never.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 13 Implementing.
Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
COMP 121 Week 11: Linked Lists.
Lecture Objectives  Linked list data structures:  Singly-linked (cont.)  Doubly-linked  Circular  Implementing the List interface as a linked list.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 8 Lists, Iterators, and Doubly Linked Lists.
© 2006 Pearson Addison-Wesley. All rights reserved5 B-1 Chapter 5 (continued) Linked Lists.
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
List Interface and Linked List Mrs. Furman March 25, 2010.
Chapter 5 Array-Based Structures © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
1 Iterators & the Collection Classes. 2 » The Collection Framework classes provided in the JAVA API(Application Programmer Interface) contains many type.
Collections ABCD ABCD Head Node Tail Node array doubly linked list Traditional Arrays and linked list: Below is memory representation of traditional.
Sets and Maps Chapter 9.
Sixth Lecture ArrayList Abstract Class and Interface
Linked Lists Chapter 5 (continued)
CE 221 Data Structures and Algorithms
John Hurley Cal State LA
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Chapter 20 Lists, Stacks, Queues, and Priority Queues
JAVA COLLECTIONS LIBRARY
COMP 121 Week 9: ArrayList.
Linked Lists.
List Data Structure.
ArrayLists.
Programming II (CS300) Chapter 07: Linked Lists and Iterators
CS2013 Lecture 4 John Hurley Cal State LA.
Dynamic Data Structures and Generics
Object Oriented Programming in java
Chapter 24 Implementing Lists, Stacks, Queues, and Priority Queues
Collections Not in our text.
Lists and the Collection Interface
Recall What is a Data Structure Very Fundamental Data Structures
Dynamic Data Structures and Generics
Chapter 4 Queues.
Sets and Maps Chapter 9.
Linked Lists Chapter 5 (continued)
Iterators Dan Fleck.
Linked Lists Chapter 5 (continued)
The List Container and Iterators
Presentation transcript:

Lists and the Collection Interface Chapter 4

Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface To understand how to write an array-based implementation of the List interface To study the difference between single-, double-, and circular linked list data structures To learn how to implement the List interface using a linked-list To understand the Iterator interface

Chapter 4: Lists and the Collection Interface3 Chapter Objective (continued) To learn how to implement the iterator for a linked list To become familiar with the Java Collection framework

Chapter 4: Lists and the Collection Interface4 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

Chapter 4: Lists and the Collection Interface5 The List Interface and ArrayList Class (continued) 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 Not all classes perform the allowed operations with the same degree of efficiency An array provides the ability to store primitive-type data whereas the List classes all store references to Objects

Chapter 4: Lists and the Collection Interface6 The List Interface and ArrayList Class (continued)

Chapter 4: Lists and the Collection Interface7 The ArrayList Class 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

Chapter 4: Lists and the Collection Interface8 The ArrayList Class (continued)

Chapter 4: Lists and the Collection Interface9 Specification of the ArrayList Class

Chapter 4: Lists and the Collection Interface10 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

Chapter 4: Lists and the Collection Interface11 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

Chapter 4: Lists and the Collection Interface12 Implementation of an ArrayList Class (continued)

Chapter 4: Lists and the Collection Interface13 Performance of KWArrayList and the Vector Class Set and get methods execute in constant time Inserting or removing elements is linear time 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

Chapter 4: Lists and the Collection Interface14 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 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

Chapter 4: Lists and the Collection Interface15 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

Chapter 4: Lists and the Collection Interface16 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)

Chapter 4: Lists and the Collection Interface17 Double-Linked Lists (continued)

Chapter 4: Lists and the Collection Interface18 Double-Linked Lists (continued)

Chapter 4: Lists and the Collection Interface19 Inserting into a Double-Linked List

Chapter 4: Lists and the Collection Interface20 Removing from a Double-Linked List

Chapter 4: Lists and the Collection Interface21 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!

Chapter 4: Lists and the Collection Interface22 Circular Lists Continued

Chapter 4: Lists and the Collection Interface23 The LinkedList Class Part of the Java API Implements the List interface using a double-linked list

Chapter 4: Lists and the Collection Interface24 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

Chapter 4: Lists and the Collection Interface25 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

Chapter 4: Lists and the Collection Interface26 The ListIterator Interface (continued)

Chapter 4: Lists and the Collection Interface27 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

Chapter 4: Lists and the Collection Interface28 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

Chapter 4: Lists and the Collection Interface29 Implementation of a Double-Linked List

Chapter 4: Lists and the Collection Interface30 Implementation of a Double-Linked List (continued)

Chapter 4: Lists and the Collection Interface31 Implementation of a Double-Linked List (continued)

Chapter 4: Lists and the Collection Interface32 Application of the LinkedList Class Case study that uses the Java LinkedList class to solve a common problem: maintaining an ordered list

Chapter 4: Lists and the Collection Interface33 Application of the LinkedList Class (continued)

Chapter 4: Lists and the Collection Interface34 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

Chapter 4: Lists and the Collection Interface35 The Collection Hierarchy (continued)

Chapter 4: Lists and the Collection Interface36 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

Chapter 4: Lists and the Collection Interface37 Chapter Review The List is a generalization of the array The Java API provides the ArrayList class, which uses an array as the underlying structure to implement the List A linked list consists of a set of nodes, each of which contains its data and a reference to the next node To find an item at a position indicated by an index in a linked list requires traversing the list from the beginning until the item at the specified index is found An iterator gives with the ability to access the items in a List sequentially

Chapter 4: Lists and the Collection Interface38 Chapter Review (continued) The ListIterator interface is an extension of the Iterator interface The Java API provides the LinkedList class, which uses a double-linked list to implement the List interface The Collection interface is the root of the Collection hierarchy The Collection interface and the List interface define a large number of methods that make these abstractions useful for many applications