DOC PREVIEW
UB CSE 115 - Collections & Definite Loops

This preview shows page 1-2-3-4-5-6 out of 18 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 18 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Collections & Definite LoopsDiscussion of PacManCollectionsJava Collections FrameworkThe Collection<E> interfaceUsing Generic TypesA usable bagAnother important collection operationFor-each loopArrayLists and PacManA useful associationUseful operations with HashMapsUsing for-each with HashMaps (note .values())The keyword forLoops (Iteration/Repetition)Definite LoopEntry Test LoopSyntax of for-loopCollections & Collections & Definite LoopsDefinite LoopsCSE 115 CSE 115 Spring 2006Spring 2006April 10, 12, & 14 2006April 10, 12, & 14 2006Discussion of PacManDiscussion of PacManMake note of the requirements listed in Make note of the requirements listed in the lab description.the lab description.Attend recitations for additional advice Attend recitations for additional advice and assistance.and assistance.In class, we will build a game (Tic Tac In class, we will build a game (Tic Tac Toe).Toe).CollectionsCollectionsStorage for many objects.Storage for many objects.Two main types:Two main types:BagsBagsAssociationsAssociationsWe will discuss use of collections, you We will discuss use of collections, you will see how to write your own collection will see how to write your own collection classes in CSE 116 & 250.classes in CSE 116 & 250.Java Collections Java Collections FrameworkFrameworkJava provides implementations for a Java provides implementations for a number of “standard” collections classes number of “standard” collections classes in the java.util package.in the java.util package.The root interface of the collections The root interface of the collections hierarchy is Collection.hierarchy is Collection.The Collection<E> The Collection<E> interfaceinterfaceNote the <E> after the word collection.Note the <E> after the word collection.The <E> indicates that this class can use The <E> indicates that this class can use a generic type (parameterized type).a generic type (parameterized type).When you create an instance of a class When you create an instance of a class with a generic type, you specify in the <> with a generic type, you specify in the <> the actual type for the generic.the actual type for the generic.Using Generic TypesUsing Generic TypesFor collections, what you are specifying For collections, what you are specifying with the generic type is the type of with the generic type is the type of objects you will be storing in a collection.objects you will be storing in a collection.Eg) I want a bag of cats.Eg) I want a bag of cats.When you do this, Java ensures that only When you do this, Java ensures that only objects of the type specified go in and objects of the type specified go in and you can be assured that only objects of you can be assured that only objects of that type come out.that type come out.A usable bagA usable bagjava.util.ArrayList<E>java.util.ArrayList<E>Note the operations that you can perform Note the operations that you can perform on this collection.on this collection.The most important will be creating an The most important will be creating an instance of the collection, inserting instance of the collection, inserting elements, removing elements, and elements, removing elements, and finding if elements are in the collection.finding if elements are in the collection.Another important Another important collection operationcollection operationIterating over all the elements of a Iterating over all the elements of a collection and performing some operation collection and performing some operation with/on each element of the collection.with/on each element of the collection.This process can be accomplished by This process can be accomplished by using a special object provided by Java using a special object provided by Java called an iterator.called an iterator.In Java 5, the use of the iterator has been In Java 5, the use of the iterator has been replaced with the for-each loop.replaced with the for-each loop.For-each loopFor-each loopAllows access to each element of a collection.Allows access to each element of a collection.Syntax:Syntax:for(TypeOfElementInCollection for(TypeOfElementInCollection giveNameToElement: NameOfCollection) {giveNameToElement: NameOfCollection) { //write code for what to do with //write code for what to do with each //element.each //element.}}ArrayLists and PacManArrayLists and PacManNote that in the Note that in the CSE115.Pacman.BoardPositions class CSE115.Pacman.BoardPositions class there are ArrayLists for each of the type there are ArrayLists for each of the type of cells on the PacMan board. Further of cells on the PacMan board. Further explanation will be provided in recitation.explanation will be provided in recitation.A useful associationA useful associationjava.util.HashMap<K, V>java.util.HashMap<K, V>Associates a key with a value. Associates a key with a value. Both the key and value are objects.Both the key and value are objects.User specifies what type of key and User specifies what type of key and value is used when HashMap is created.value is used when HashMap is created.Useful operations with Useful operations with HashMapsHashMapsput (put a key/value pair in the HashMap)put (put a key/value pair in the HashMap)RemoveRemoveLook up a value using its keyLook up a value using its keyIterating using the for-each loopIterating using the for-each loopUsing for-each with Using for-each with HashMaps (note HashMaps (note .values().values()))java.util.HashMap<Position, Cell> _board = java.util.HashMap<Position, Cell> _board = new java.util.HashMap <Position, Cell>();new java.util.HashMap <Position, Cell>();//magic happens to put things into //magic happens to put things into board.board.for(Cell c: _board.values() {for(Cell c: _board.values() {c.draw();c.draw();}}The keyword The keyword forforThe for-each is a specialized loop The for-each is a specialized loop designed to work with collections.designed to work with collections.forfor is a keyword in Java that tells us is a keyword in Java that tells us there is a loop.there is a loop.You can create a regular “for-loop” for You can create a regular “for-loop” for use in your programs.use in your programs.Loops Loops (Iteration/Repetition)(Iteration/Repetition)The ability to do a task repeatedly.The ability to do a task repeatedly.The functionality of repetition is most The functionality of repetition is most often implemented in programming


View Full Document

UB CSE 115 - Collections & Definite Loops

Download Collections & Definite Loops
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Collections & Definite Loops and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Collections & Definite Loops 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?