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

Generics and the ArrayList Class
Comp 249 Programming Methodology Chapter 7 - Inheritance – Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
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 7 Inheritance.
Slides prepared by Rose Williams, Binghamton University Chapter 9 Exception Handling.
Chapter 2 Application Layer. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 2-2.
Slides prepared by Rose Williams, Binghamton University Chapter 17 Swing I.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
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 5 Defining Classes II.
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.
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.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
CS102--Object Oriented Programming Lecture 6: – The Arrays class – Multi-dimensional arrays Copyright © 2008 Xiaoyan Li.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved L15 (Chapter 22) Java Collections.
Chapter 6 Human Capital. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. 6-2.
Slides prepared by Rose Williams, Binghamton University Chapter 9 More Exception Handling.
Slides prepared by Rose Williams, Binghamton University Chapter 11 Recursion.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Slides prepared by Rose Williams, Binghamton University Chapter 16 Collections and Iterators.
Slides prepared by Rose Williams, Binghamton University Chapter 7 Inheritance.
Chapter 16 Resources and the Environment at the Global Level.
Slides prepared by Rose Williams, Binghamton University Chapter 13 Interfaces and Inner Classes.
1 Introduction to Searching and Sorting Comparable Interface -Reading p Comparator Interface.
Chapter 3 Describing Syntax and Semantics. Copyright © 2007 Addison-Wesley. All rights reserved 3–2.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 22 Lists, Stacks, Queues, and Priority.
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
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.
CMSC 202 Generics. Nov Generalized Code One goal of OOP is to provide the ability to write reusable, generalized code. Polymorphic code using.
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
Comp 249 Programming Methodology Chapter 13 Interfaces & Inner Classes Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
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.
Chapter 15 Linked Data Structures Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 20 Java Never Ends.
Chapter 13 Interfaces and Inner Classes Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
© 2006 Pearson Addison-Wesley. All rights reserved 1-1 Chapter 1 Review of Java Fundamentals.
Arrays (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Comp 248 Introduction to Programming Chapter 6 Arrays Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Collections, Maps and Iterators
CMSC 202 ArrayList Aug 9, 2007.
CMSC 202 Interfaces.
CMSC 202 ArrayList Aug 9, 2007.
Object-Oriented Programming Paradigm
Comp 249 Programming Methodology
Object Oriented Programming in java
CMSC 202 ArrayList Aug 9, 2007.
CMSC 202 Interfaces.
CMSC 202 Generics.
Containers and Iterators
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
Chapter 4 Constructors Section 4.4
Chapter 6 Arrays.
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 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-3 The Collection Landscape

© 2006 Pearson Addison-Wesley. All rights reserved16-4 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-5 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-6 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-7 Method Headings in the Collection Interface ( Part 1 of 10)

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

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

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

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

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

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

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

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

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

© 2006 Pearson Addison-Wesley. All rights reserved16-17 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-18 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-19 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-20 Methods in the Set Interface (Part 1 of 10)

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

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

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

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

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

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

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

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

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