Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.

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

Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
Generics and the ArrayList Class
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
Chapter 16 Collections, Maps and Iterators Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
© 2006 Pearson Addison-Wesley. All rights reserved16-1 Iterators An iterator is an object that is used with a collection to provide sequential access to.
Slides prepared by Rose Williams, Binghamton University Chapter 9 Exception Handling.
Slides prepared by Rose Williams, Binghamton University Chapter 20 Java Threads Part II.
© 2006 Pearson Addison-Wesley. All rights reserved16-1 Methods in the List Interface (Part 1 of 16)
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Chapter 13 Interfaces and Inner Classes Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
Using ArrayList. Lecture Objectives To understand the foundations behind the ArrayList class Explore some of the methods of the ArrayList class.
Java Collections. Lecture Objectives To understand the concepts of Java collections To be able to implement Java programs based on Collections.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 24 : Collections King Fahd University of Petroleum & Minerals College of Computer.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
Iterators in Java. Lecture Objectives To understand the concepts of Java iterators To understand the differences between the Iterator and ListIterator.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
Unit 291 Java Collections Framework: Interfaces Introduction to the Java Collections Framework (JCF) The Comparator Interface Revisited The Collection.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
1 Introduction to Searching and Sorting Comparable Interface -Reading p Comparator Interface.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 22 Lists, Stacks, Queues, and Priority.
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
Slides prepared by Rose Williams, Binghamton University ICS201 Lecture 9 : Exception Handling King Fahd University of Petroleum & Minerals College of Computer.
Comp 249 Programming Methodology Chapter 8 - Polymorphism Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University, Montreal,
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
CMSC 202 Exceptions. Aug 7, Error Handling In the ideal world, all errors would occur when your code is compiled. That won’t happen. Errors which.
Copyright © 2002, Systems and Computer Engineering, Carleton University Patterns.ppt * Object-Oriented Software Development Part 11.
CMSC 202 Generics. Nov Generalized Code One goal of OOP is to provide the ability to write reusable, generalized code. Polymorphic code using.
GENERIC COLLECTIONS. Type-Wrapper Classes  Each primitive type has a corresponding type- wrapper class (in package java.lang).  These classes are called.
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
Slides prepared by Rose Williams, Binghamton University Chapter 15 Linked Data Structures.
Comp 249 Programming Methodology Chapter 13 Interfaces & Inner Classes Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics.
Introduction to Generics
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
Comp 249 Programming Methodology Chapter 15 Linked Data Structure – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Chapter 15 Linked Data Structures Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 20 Lists, Stacks, Queues, and Priority.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 8 Lists, Iterators, and Doubly Linked Lists.
CSE 1201 Object Oriented Programming ArrayList 1.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Chapter 13 Interfaces and Inner Classes Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
Copyright 2006 Pearson Addison-Wesley, 2008, 2012 Joey Paquet 1 Concordia University Department of Computer Science and Software Engineering SOEN6441 –
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Java Generics. Lecture Objectives To understand the objective of generic programming To be able to implement generic classes and methods To know the limitations.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
Collections, Maps and Iterators
CMSC 202 ArrayList Aug 9, 2007.
Chapter 20 Generic Classes and Methods
Comp 249 Programming Methodology
Comp 249 Programming Methodology
CMSC 202 Interfaces.
CMSC 202 ArrayList Aug 9, 2007.
Comp 249 Programming Methodology
Interfaces and Inner Classes
CMSC 202 ArrayList Aug 9, 2007.
CMSC 202 Interfaces.
CMSC 202 Generics.
Containers and Iterators
Comp 249 Programming Methodology
CMSC 202 Interfaces.
Presentation transcript:

Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators

© 2006 Pearson Addison-Wesley. All rights reserved16-2 Reading Assignment: Chapter 16 from the pdf file attached

© 2006 Pearson Addison-Wesley. All rights reserved16-3 Collections A Java collection is any class that holds objects and implements the Collection interface –For example, the ArrayList class is a Java collection class, and implements all the methods in the Collection interface –Collections are used along with iterators The Collection interface is the highest level of Java's framework for collection classes –All of the collection classes discussed here can be found in package java.util

© 2006 Pearson Addison-Wesley. All rights reserved16-4 The Collection Landscape

© 2006 Pearson Addison-Wesley. All rights reserved16-5 Wildcards Classes and interfaces in the collection framework can have parameter type specifications that do not fully specify the type plugged in for the type parameter –Because they specify a wide range of argument types, they are known as wildcards public void method(String arg1, ArrayList arg2) –In the above example, the first argument is of type String, while the second argument can be an ArrayList with any base type

© 2006 Pearson Addison-Wesley. All rights reserved16-6 Wildcards A bound can be placed on a wildcard specifying that the type used must be an ancestor type or descendent type of some class or interface –The notation specifies that the argument plugged in be an object of any descendent class of String –The notation specifies that the argument plugged in be an object of any ancestor class of String

© 2006 Pearson Addison-Wesley. All rights reserved16-7 The Collection Framework The Collection interface describes the basic operations that all collection classes should implement –The method headings for these operations are shown on the next several slides Since an interface is a type, any method can be defined with a parameter of type Collection –That parameter can be filled with an argument that is an object of any class in the collection framework

© 2006 Pearson Addison-Wesley. All rights reserved16-8 Method Headings in the Collection Interface ( Part 1 of 10)

© 2006 Pearson Addison-Wesley. All rights reserved16-9 Method Headings in the Collection Interface ( Part 2 of 10)

© 2006 Pearson Addison-Wesley. All rights reserved16-10 Method Headings in the Collection Interface ( Part 3 of 10)

© 2006 Pearson Addison-Wesley. All rights reserved16-11 Method Headings in the Collection Interface ( Part 4 of 10)

© 2006 Pearson Addison-Wesley. All rights reserved16-12 Method Headings in the Collection Interface ( Part 5 of 10)

© 2006 Pearson Addison-Wesley. All rights reserved16-13 Method Headings in the Collection Interface ( Part 6 of 10)

© 2006 Pearson Addison-Wesley. All rights reserved16-14 Method Headings in the Collection Interface ( Part 7 of 10)

© 2006 Pearson Addison-Wesley. All rights reserved16-15 Method Headings in the Collection Interface ( Part 8 of 10)

© 2006 Pearson Addison-Wesley. All rights reserved16-16 Method Headings in the Collection Interface ( Part 9 of 10)

© 2006 Pearson Addison-Wesley. All rights reserved16-17 Method Headings in the Collection Interface ( Part 10 of 10)

© 2006 Pearson Addison-Wesley. All rights reserved16-18 Collection Relationships There are a number of different predefined classes that implement the Collection interface –Programmer defined classes can implement it also A method written to manipulate a parameter of type Collection will work for all of these classes, either singly or intermixed There are two main interfaces that extend the Collection interface: The Set interface and the List interface

© 2006 Pearson Addison-Wesley. All rights reserved16-19 Collection Relationships Classes that implement the Set interface do not allow an element in the class to occur more than once –The Set interface has the same method headings as the Collection interface, but in some cases the semantics (intended meanings) are different –Methods that are optional in the Collection interface are required in the Set interface

© 2006 Pearson Addison-Wesley. All rights reserved16-20 Collection Relationships Classes that implement the List interface have their elements ordered as on a list –Elements are indexed starting with zero –A class that implements the List interface allows elements to occur more than once –The List interface has more method headings than the Collection interface –Some of the methods inherited from the Collection interface have different semantics in the List interface –The ArrayList class implements the List interface

© 2006 Pearson Addison-Wesley. All rights reserved16-21 Methods in the Set Interface (Part 1 of 10)

© 2006 Pearson Addison-Wesley. All rights reserved16-22 Methods in the Set Interface (Part 2 of 10)

© 2006 Pearson Addison-Wesley. All rights reserved16-23 Methods in the Set Interface (Part 3 of 10)

© 2006 Pearson Addison-Wesley. All rights reserved16-24 Methods in the Set Interface (Part 4 of 10)

© 2006 Pearson Addison-Wesley. All rights reserved16-25 Methods in the Set Interface (Part 5 of 10)

© 2006 Pearson Addison-Wesley. All rights reserved16-26 Methods in the Set Interface (Part 6 of 10)

© 2006 Pearson Addison-Wesley. All rights reserved16-27 Methods in the Set Interface (Part 7 of 10)

© 2006 Pearson Addison-Wesley. All rights reserved16-28 Methods in the Set Interface (Part 8 of 10)

© 2006 Pearson Addison-Wesley. All rights reserved16-29 Methods in the Set Interface (Part 9 of 10)

© 2006 Pearson Addison-Wesley. All rights reserved16-30 Methods in the Set Interface (Part 10 of 10)

© 2006 Pearson Addison-Wesley. All rights reserved16-31 Methods in the List Interface (Part 1 of 16)

© 2006 Pearson Addison-Wesley. All rights reserved16-32 Methods in the List Interface (Part 2 of 16)

© 2006 Pearson Addison-Wesley. All rights reserved16-33 Methods in the List Interface (Part 3 of 16)

© 2006 Pearson Addison-Wesley. All rights reserved16-34 Methods in the List Interface (Part 4 of 16)

© 2006 Pearson Addison-Wesley. All rights reserved16-35 Methods in the List Interface (Part 5 of 16)

© 2006 Pearson Addison-Wesley. All rights reserved16-36 Methods in the List Interface (Part 6 of 16)

© 2006 Pearson Addison-Wesley. All rights reserved16-37 Methods in the List Interface (Part 7 of 16)

© 2006 Pearson Addison-Wesley. All rights reserved16-38 Methods in the List Interface (Part 8 of 16)

© 2006 Pearson Addison-Wesley. All rights reserved16-39 Methods in the List Interface (Part 9 of 16)

© 2006 Pearson Addison-Wesley. All rights reserved16-40 Methods in the List Interface (Part 10 of 16)

© 2006 Pearson Addison-Wesley. All rights reserved16-41 Methods in the List Interface (Part 11 of 16)

© 2006 Pearson Addison-Wesley. All rights reserved16-42 Methods in the List Interface (Part 12 of 16)

© 2006 Pearson Addison-Wesley. All rights reserved16-43 Methods in the List Interface (Part 13 of 16)

© 2006 Pearson Addison-Wesley. All rights reserved16-44 Methods in the List Interface (Part 14 of 16)

© 2006 Pearson Addison-Wesley. All rights reserved16-45 Methods in the List Interface (Part 15 of 16)

© 2006 Pearson Addison-Wesley. All rights reserved16-46 Methods in the List Interface (Part 16 of 16)

© 2006 Pearson Addison-Wesley. All rights reserved16-47 Pitfall: Optional Operations When an interface lists a method as "optional," it must still be implemented in a class that implements the interface –The optional part means that it is permitted to write a method that does not completely implement its intended semantics –However, if a trivial implementation is given, then the method body should throw an UnsupportedOperationException

© 2006 Pearson Addison-Wesley. All rights reserved16-48 Tip: Dealing with All Those Exceptions The tables of methods for the various collection interfaces and classes indicate that certain exceptions are thrown –These are unchecked exceptions, so they are useful for debugging, but need not be declared or caught In an existing collection class, they can be viewed as run-time error messages In a derived class of some other collection class, most or all of them will be inherited In a collection class defined from scratch, if it is to implement a collection interface, then it should throw the exceptions that are specified in the interface

© 2006 Pearson Addison-Wesley. All rights reserved16-49 Concrete Collections Classes The ArrayList and Vector classes implement the List interface, and can be used if additional methods are not needed –Both the ArrayList and Vector classes implement all the methods in the interface List –Either class can be used when a List with efficient random access to elements is needed

© 2006 Pearson Addison-Wesley. All rights reserved16-50 Concrete Collections Classes The concrete class LinkedList is a concrete derived class of the abstract class AbstractSequentialList –When efficient sequential movement through a list is needed, the LinkedList class should be used

© 2006 Pearson Addison-Wesley. All rights reserved16-51 Methods in the Classes ArrayList and Vector (Part 1 of 15)

© 2006 Pearson Addison-Wesley. All rights reserved16-52 Methods in the Classes ArrayList and Vector (Part 2 of 15)

© 2006 Pearson Addison-Wesley. All rights reserved16-53 Methods in the Classes ArrayList and Vector (Part 3 of 15)

© 2006 Pearson Addison-Wesley. All rights reserved16-54 Methods in the Classes ArrayList and Vector (Part 4 of 15)

© 2006 Pearson Addison-Wesley. All rights reserved16-55 Methods in the Classes ArrayList and Vector (Part 5 of 15)

© 2006 Pearson Addison-Wesley. All rights reserved16-56 Methods in the Classes ArrayList and Vector (Part 6 of 15)

© 2006 Pearson Addison-Wesley. All rights reserved16-57 Methods in the Classes ArrayList and Vector (Part 7 of 15)

© 2006 Pearson Addison-Wesley. All rights reserved16-58 Methods in the Classes ArrayList and Vector (Part 8 of 15)

© 2006 Pearson Addison-Wesley. All rights reserved16-59 Methods in the Classes ArrayList and Vector (Part 9 of 15)

© 2006 Pearson Addison-Wesley. All rights reserved16-60 Methods in the Classes ArrayList and Vector (Part 10 of 15)

© 2006 Pearson Addison-Wesley. All rights reserved16-61 Methods in the Classes ArrayList and Vector (Part 11 of 15)

© 2006 Pearson Addison-Wesley. All rights reserved16-62 Methods in the Classes ArrayList and Vector (Part 12 of 15)

© 2006 Pearson Addison-Wesley. All rights reserved16-63 Methods in the Classes ArrayList and Vector (Part 13 of 15)

© 2006 Pearson Addison-Wesley. All rights reserved16-64 Methods in the Classes ArrayList and Vector (Part 14 of 15)

© 2006 Pearson Addison-Wesley. All rights reserved16-65 Methods in the Classes ArrayList and Vector (Part 15 of 15)

© 2006 Pearson Addison-Wesley. All rights reserved16-66 Differences Between ArrayList and Vector For most purposes, the ArrayList and Vector are equivalent –The Vector class is older, and had to be retrofitted with extra method names to make it fit into the collection framework –The ArrayList class is newer, and was created as part of the Java collection framework –The ArrayList class is supposedly more efficient than the Vector class also

© 2006 Pearson Addison-Wesley. All rights reserved16-67 Pitfall: Omitting the When the or corresponding class name is omitted from a reference to a collection class, this is an error for which the compiler may or may not issue an error message (depending on the details of the code), and even if it does, the error message may be quite strange –Look for a missing or when a program that uses collection classes gets a strange error message or doesn't run correctly

© 2006 Pearson Addison-Wesley. All rights reserved16-68 Iterators An iterator is an object that is used with a collection to provide sequential access to the collection elements –This access allows examination and possible modification of the elements An iterator imposes an ordering on the elements of a collection even if the collection itself does not impose any order on the elements it contains –If the collection does impose an ordering on its elements, then the iterator will use the same ordering

© 2006 Pearson Addison-Wesley. All rights reserved16-69 The Iterator Interface Java provides an Iterator interface –Any object of any class that satisfies the Iterator interface is an Iterator An Iterator does not stand on its own –It must be associated with some collection object using the method iterator –If c is an instance of a collection class, the following can be used to visit all the elements of c : Iterator iter = c.iterator(); while(iter.hasNext( )){ Object obj = iter.next(); process(obj); }

© 2006 Pearson Addison-Wesley. All rights reserved16-70 Methods in the Iterator Interface (Part 1 of 2) Note: When next() is invoked, the iterator jumps over the next element, and it returns a reference to the object that it just passed.

© 2006 Pearson Addison-Wesley. All rights reserved16-71 Methods in the Iterator Interface (Part 2 of 2) Note: It is illegal to call remove() of an iterator if it was not preceded by a call to next().

© 2006 Pearson Addison-Wesley. All rights reserved16-72 Using an Iterator with an ArrayList Object (1/2) 1.import java.util.*; 2.public class TestIterator { 3. public static void main(String[] args) { 4. ArrayList list = new ArrayList (); 5. Iterator iter; 6. for(int i = 0; i < 6; i++) 7. list.add(new Integer(i)); System.out.println("list before: "+list); iter = list.iterator(); 12. if (iter.hasNext()) { 13. iter.next(); 14. } 15. if (iter.hasNext()) { 16. iter.next(); 17. } 18. iter.remove(); 19. System.out.println("list after first removal: "+list);

© 2006 Pearson Addison-Wesley. All rights reserved16-73 Using an Iterator with an ArrayList Object (2/2) 20./* 21. // The following will compile, but will raise an Exception 22. iter.remove(); 23. // It had to be preceded by an iter.next() 24.*/ iter = list.iterator(); // Have to “reset” the iterator while (iter.hasNext()) { 29. Integer element = (Integer)iter.next(); 30. System.out.println(element); 31. } 32. } 33.}

© 2006 Pearson Addison-Wesley. All rights reserved16-74 Tip: For-Each Loops as Iterators Although it is not an iterator, a for-each loop can serve the same purpose as an iterator –A for-each loop can be used to cycle through each element in a collection For-each loops can be used with any of the collections discussed here

© 2006 Pearson Addison-Wesley. All rights reserved16-75 Example 1.import java.util.*; 2.public class ForEachDemo2 { 3. public static void main(String[] args) { 4. ArrayList list = new ArrayList (); 5. for(int i = 0; i < 6; i++) 6. list.add(new Integer(i)); System.out.println("list before: "+list); for (Integer element : list) { 11. System.out.println(element); 12. } 13. } 14.}

© 2006 Pearson Addison-Wesley. All rights reserved16-76 The ListIterator Interface The ListIterator interface extends the Iterator interface, and is designed to work with collections that satisfy the List interface –A ListIterator has all the methods that an Iterator has, plus additional methods –A ListIterator can move in either direction along a list of elements –A ListIterator has methods, such as set and add, that can be used to modify elements

© 2006 Pearson Addison-Wesley. All rights reserved16-77 Methods in the ListIterator Interface (Part 1 of 4)

© 2006 Pearson Addison-Wesley. All rights reserved16-78 Methods in the ListIterator Interface (Part 2 of 4)

© 2006 Pearson Addison-Wesley. All rights reserved16-79 Methods in the ListIterator Interface (Part 3 of 4)

© 2006 Pearson Addison-Wesley. All rights reserved16-80 Methods in the ListIterator Interface (Part 4 of 4)

© 2006 Pearson Addison-Wesley. All rights reserved16-81 The ListIterator Cursor Every ListIterator has a position marker known as the cursor –If the list has n elements, they are numbered by indices 0 through n-1, but there are n+1 cursor positions –When next() is invoked, the element immediately following the cursor position is returned and the cursor is moved forward one cursor position –When previous() is invoked, the element immediately before the cursor position is returned and the cursor is moved back one cursor position

© 2006 Pearson Addison-Wesley. All rights reserved16-82 ListIterator Cursor Positions

© 2006 Pearson Addison-Wesley. All rights reserved16-83 Pitfall: next and previous Can Return a Reference Theoretically, when an iterator operation returns an element of the collection, it might return a copy or clone of the element, or it might return a reference to the element Iterators for the standard predefined collection classes, such as ArrayList and HashSet, actually return references –Therefore, modifying the returned value will modify the element in the collection

© 2006 Pearson Addison-Wesley. All rights reserved16-84 An Iterator Returns a Reference (Part 1 of 4)

© 2006 Pearson Addison-Wesley. All rights reserved16-85 An Iterator Returns a Reference (Part 2 of 4)

© 2006 Pearson Addison-Wesley. All rights reserved16-86 An Iterator Returns a Reference (Part 3 of 4)

© 2006 Pearson Addison-Wesley. All rights reserved16-87 An Iterator Returns a Reference (Part 4 of 4)