 A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with.

Slides:



Advertisements
Similar presentations
Chapter 5 introduces the often- used data structure of linked lists. This presentation shows how to implement the most common operations on linked lists.
Advertisements

CSC211 Data Structures Lecture 9 Linked Lists Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach to the.
Chapter 8 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences of a problem.
Container Classes A container class is a data type that is capable of holding a collection of items. In C++, container classes can be implemented as.
1 CSC211 Data Structures Lectures 4 & 5 Container Classes Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
L l One of the tree applications in Chapter 9 is binary search trees. l l In Chapter 9, binary search trees are used to implement bags and sets. l l This.
 One of the tree applications in Chapter 10 is binary search trees.  In Chapter 10, binary search trees are used to implement bags and sets.  This presentation.
CSC212 Data Structure Lecture 14 Binary Search Trees Instructor: Prof. George Wolberg Department of Computer Science City College of New York.
 Chapter 2 introduces Object Oriented Programming.  OOP is a relatively new approach to programming which supports the creation of new data types and.
 Chapter 11 has several programming projects, including a project that uses heaps.  This presentation shows you what a heap is, and demonstrates two.
CSC212 Data Structure - Section AB Lecture 20 Hashing Instructor: Edgardo Molina Department of Computer Science City College of New York.
Introduction This chapter explores graphs and their applications in computer science This chapter explores graphs and their applications in computer science.
 Chapter 7 introduces the stack data type.  Several example applications of stacks are given in that chapter.  This presentation shows another use called.
P Chapter 6 introduces the stack data type. p Several example applications of stacks are given in that chapter. p This presentation shows another use called.
  Chapter 12 presents several common algorithms for sorting an array of integers.   Two slow but simple algorithms are Selectionsort and Insertionsort.
P p Chapter 10 has several programming projects, including a project that uses heaps. p p This presentation shows you what a heap is, and demonstrates.
1 CSC212 Data Structure - Section FG Lectures 4 & 5 Container Classes Instructor: Zhigang Zhu Department of Computer Science City College of New York.
CSC212 Data Structure - Section FG Lecture 21 Quadratic Sorting Instructor: Zhigang Zhu Department of Computer Science City College of New York.
 Chapter 10 introduces trees.  This presentation illustrates the simplest kind of trees: Complete Binary Trees. Complete Binary Trees Data Structures.
CSC212 Data Structure - Section FG Lecture 9 Linked Lists Instructor: Zhigang Zhu Department of Computer Science City College of New York.
Copyright © 1999, Carnegie Mellon. All Rights Reserved. Chapter 12 presents several common algorithms for sorting an array of integers. Two slow but simple.
L l Chapter 11 discusses several ways of storing information in an array, and later searching for the information. l l Hash tables are a common approach.
 An important topic: preconditions and postconditions.  They are a method of specifying what a function accomplishes. Preconditions and Postconditions.
L l Chapter 4 introduces the often- used linked list data structures l l This presentation shows how to implement the most common operations on linked.
 Chapter 9 introduces the technique of recursive programming.  As you have seen, recursive programming involves spotting smaller occurrences of a problem.
P Chapter 2 introduces Object Oriented Programming. p OOP is a relatively new approach to programming which supports the creation of new data types and.
CSC212 Data Structure - Section FG Lecture 12 Stacks and Queues Instructor: Zhigang Zhu Department of Computer Science City College of New York.
 Chapter 8 introduces the queue data type.  Several example applications of queues are given in that chapter.  This presentation describes the queue.
Sorting Sorting Arranging items in a collection so that there is an ordering on one (or more) of the fields in the items Sort Key The field (or fields)
P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Preconditions and Postconditions.
P An important topic: preconditions and postconditions. p They are a method of specifying what a function accomplishes. Illinois State University Department.
CSC211 Data Structures Lecture 20 Hashing Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
 An important topic: preconditions and postconditions.  They are a method of specifying what a method accomplishes. Preconditions and Postconditions.
Software Development p Data structure p A collection of data organized so that the data can be accessed using a set of specific techniques p Object-oriented.
CSC211 Data Structures Lecture 21 Quadratic Sorting Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
CSC211 Data Structures Lecture 18 Heaps and Priority Queues Instructor: Prof. Xiaoyan Li Department of Computer Science Mount Holyoke College.
P p Chapter 10 introduces trees. p p This presentation illustrates the simplest kind of trees: Complete Binary Trees. Complete Binary Trees Data Structures.
Container Classes  A container class is a data type that is capable of holding a collection of items.  In C++, container classes can be implemented as.
CSC212 Data Structure Lecture 16 Heaps and Priority Queues Instructor: George Wolberg Department of Computer Science City College of New York.
1 CSC212 Data Structure Lecture 4 Container Classes Instructor: George Wolberg Department of Computer Science City College of New York.
 Chapter 6 introduces templates, which are a C++ feature that easily permits the reuse of existing code for new purposes.  This presentation shows how.
Linked Lists in Action Chapter 5 introduces the often-used data public classure of linked lists. This presentation shows how to implement the most common.
Collection Classes A Collection class is a data type that is capable of holding a group of items. In Java, Collection classes can be implemented as a class,
Using a Stack Chapter 6 introduces the stack data type.
Hash Tables Chapter 11 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach.
Using a Stack Chapter 6 introduces the stack data type.
Quadratic Sorting Chapter 12 presents several common algorithms for sorting an array of integers. Two slow but simple algorithms are Selectionsort and.
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Using a Stack Chapter 6 introduces the stack data type.
Binary Search Trees One of the tree applications in Chapter 9 is binary search trees. In Chapter 9, binary search trees are used to implement bags and.
Heaps Chapter 11 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates.
Preconditions and Postconditions
Using a Stack Chapter 6 introduces the stack data type.
Hash Tables Chapter 12 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common.
Hash Tables Chapter 11 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach.
Hash Tables Chapter 11 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach.
Binary Search Trees One of the tree applications in Chapter 9 is binary search trees. In Chapter 9, binary search trees are used to implement bags and.
Using a Queue Chapter 8 introduces the queue data type.
Linked Lists in Action Chapter 5 introduces the often- used data structure of linked lists. This presentation shows how to implement the most common.
Using a Queue Chapter 8 introduces the queue data type.
Preconditions and Postconditions
Heaps Chapter 10 has several programming projects, including a project that uses heaps. This presentation shows you what a heap is, and demonstrates two.
Complete Binary Trees Chapter 9 introduces trees.
Hash Tables Chapter 11 discusses several ways of storing information in an array, and later searching for the information. Hash tables are a common approach.
Presentation transcript:

 A Collection class is a data type that is capable of holding a group of items.  In Java, Collection classes can be implemented as a class, along with methods to add, remove, and examine items. Collection Classes Data Structures and Other Objects Using Java

Bags  For the first example, think about a bag.

Bags  For the first example, think about a bag.  Inside the bag are some numbers.

Initial State of a Bag  When you first begin to use a bag, the bag will be empty.  We count on this to be the initial state of any bag that we use. THIS BAG IS EMPTY.

Adding Numbers into a Bag  Numbers may be added into a bag. I AM PUTTING THE NUMBER 4 INTO THE BAG.

Adding Numbers into a Bag  Numbers may be added into a bag. THE 4 IS IN THE BAG.

Adding Numbers into a Bag  Numbers may be added into a bag.  The bag can hold many numbers. NOW I'M PUTTING ANOTHER NUMBER IN THE BAG -- AN 8.

added Numbers into a Bag  Numbers may be added into a bag.  The bag can hold many numbers. THE 8 IS ALSO IN THE BAG.

added Numbers into a Bag  Numbers may be added into a bag.  The bag can hold many numbers.  We can even place the same number more than once. NOW I'M PUTTING A SECOND 4 IN THE BAG.

Adding Numbers into a Bag  Numbers may be added into a bag.  The bag can hold many numbers.  We can even place the same number more than once. NOW THE BAG HAS TWO 4'S AND AN 8..

Examining a Bag  We may ask about the contents of the bag. HAVE YOU GOT ANY 4's ? YES, I HAVE TWO OF THEM.

Removing a Number from a Bag  We may remove a number from a bag. THIS 4 IS OUTTA HERE!

Removing a Number from a Bag  We may remove a number from a bag.  But we remove only one number at a time. ONE 4 IS GONE, BUT THE OTHER 4 REMAINS.

How Many Numbers  Another operation is to determine how many numbers are in a bag. IN MY OPINION, THERE ARE TOO MANY NUMBERS.

Summary of the Bag Operations  A bag can be put in its initial state, which is an empty bag.  Numbers can be added into the bag.  You may check how many occurrences of a certain number are in the bag.  Numbers can be removed from the bag.  You can check how many numbers are in the bag.

The Bag Class  Java classes (introduced in Chapter 2) can be used to implement a Collection class such as a Bag.  The class definition includes: public class Bag   The heading of the definition

The Bag Class  Java classes (introduced in Chapter 2) can be used to implement a Collection class such as a Bag.  The class definition includes: class Bag { public Bag( )...   The heading of the definition   A constructor

The Bag Class  Java classes (introduced in Chapter 2) can be used to implement a Collection class such as a Bag.  The class definition includes: public class Bag { public Bag( )... public void add(... public void remove(......and so on   The heading of the definition   A constructor   Public methods

The Bag Class  Java classes (introduced in Chapter 2) can be used to implement a Collection class such as a Bag.  The class definition includes:   The heading of the definition   A constructor   Public methods   Private instance variables We’ll look at private instance variables later. We’ll look at private instance variables later. public class Bag { public Bag( )... public void add(... public void remove(......and so on

The Bag’s Constructor  Places a bag in the initial state (an empty bag) // Postcondition: The Bag has been initialized // and it is now empty. public Bag( ) {... }

The Add Method  Adds a new number to the bag public void add(int newEntry) {... }

The Size Method  Counts how many integers are in the bag. // Postcondition: The return value is the number // of integers in the Bag. public int size( ) {... }

The countOccurrences Method  Counts how many copies of a number occur // Postcondition: The return value is the number // of copies of target in the Bag. public int countOccurrences(int target) {... }

The Remove Method  Removes one copy of a number // Postcondition: If target was in the Bag, then // one copy of target has been removed from the // Bag, and the return value is true; otherwise the // Bag is unchanged and the return value is false. public boolean remove(int target) {... }

Using the Bag in a Program  Here is typical code from a program that uses the new Bag class: Bag ages = new Bag( ); // Record the ages of three children: ages.add(4); ages.add(8); ages.add(4);

Documentation for the Bag Class  The documentation gives specifications for the bag methods.  Specifications are written as precondition/postcondition contracts.  Everything needed to use the Bag class is included in this documentation. Bag’s documentation can be automatically created with the Javadoc tool described in Appendix H.

A Quiz Suppose that a Mysterious Benefactor provides you with the Bag class, but you are only permitted to read the documentation. You cannot read the class implementation or.java file. Can you write a program that uses the Bag data type ?  Yes I can.  No. Not unless I see the class implementation for the Bag.

A Quiz Suppose that a Mysterious Benefactor provides you with the Bag class, but you are only permitted to read the documentation. You cannot read the class implementation or.java file. Can you write a program that uses the Bag data type ?  Yes I can. You know the name of the new data type, and you also know the headings and specifications of each of the operations. This is enough for you to create and use Bags. You know the name of the new data type, and you also know the headings and specifications of each of the operations. This is enough for you to create and use Bags.

Implementation Details  The entries of a bag will be stored in the front part of an array, as shown in this example. [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ]... An array of integers 484 We don't care what's in this part of the array.

Implementation Details  The entries may appear in any order. This represents the same bag as the previous one... An array of integers 448 We don't care what's in this part of the array. [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ]...

Implementation Details ... and this also represents the same bag. An array of integers We don't care what's in this part of the array. [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ]

Implementation Details  We also need to keep track of how many numbers are in the bag. An array of integers 844 We don't care what's in this part of the array. An integer to keep track of the bag's size 3 [ 0 ] [1] [ 2 ] [ 3 ] [ 4 ] [ 5 ]...

An Exercise Use these ideas to write a list of private instance variables could implement the Bag class. You should have two instance variables. You have 60 seconds to write the declaration.

An Exercise public class Bag { private int[ ] data; private int manyItems;... } One solution:

An Example of Calling Add public void add(int newEntry) Before calling add, we might have this bag b: 2 [ 0 ] [ 1 ] [2] b.data b.manyItems

An Example of Calling Add void add(int newEntry) b.data b.manyItems We activate b.add(17) What values will be in b.data and b.manyItems after the method finishes ? [ 0 ] [ 1 ] [2] public void add(int newEntry) 2

An Example of Calling Add void add(int newEntry) After activating b.add(17), we will have this bag b: 3 [ 0 ] [1] [ 2 ] public void add(int newEntry) b.data b.manyItems [ 0 ] [ 1 ] [2]

Pseudocode for add  Make sure there is room for a new entry in the array.  Place newEntry in the appropriate location of the data array.  Add one to the instance variable manyItems. What is the “appropriate location” of the data array ?

Pseudocode for add data[manyItems] = newEntry; manyItems++;  Make sure there is room for a new entry in the array.  Place newEntry in the appropriate location of the data array.  Add one to the instance variable manyItems.

Pseudocode for add data[ manyItems++] = newEntry;  Make sure there is room for a new entry in the array.  Place newEntry in the appropriate location of the data array.  Add one to the instance variable manyItems.

The Other Bag Operations  Read Section 3.2 for the implementations of the other bag methods.  Remember: If you are just using the Bag class, then you don’t need to know how the operations are implemented.  Later we will reimplement the bag using more efficient algorithms.  We’ll also have a few other operations to manipulate bags.

Other Kinds of Bags  In this example, we have implemented a bag containing integers.  But we could have had a bag of float numbers, a bag of characters, a bag of Strings... Suppose you wanted one of these other bags. How much would you need to change in the implementation ?

 A Collection class is a class that can hold a group of items.  Collection classes can be implemented with a Java class.  The author of the class should provide documentation that another programmer can read to use the class.  Other details are given in Section 3.2, which you should read. Summary

T HE E ND Presentation copyright 1999, Addison Wesley Longman For use with Data Structures and Other Objects Using Java by Michael Main. Some artwork in the presentation is used with permission from Presentation Task Force (copyright New Vision Technologies Inc.) and Corel Gallery Clipart Catalog (copyright Corel Corporation, 3G Graphics Inc., Archive Arts, Cartesia Software, Image Club Graphics Inc., One Mile Up Inc., TechPool Studios, Totem Graphics Inc.). Students and instructors who use Data Structures and Other Objects Using Java are welcome to use this presentation however they see fit, so long as this copyright notice remains intact.