Collections - PowerPoint PPT Presentation

About This Presentation
Title:

Collections

Description:

Collections & Definite Loops. CSE 115. Spring 2006. April 10, 12, & 14 2006. Discussion of PacMan ... and PacMan. Note that in the CSE115.Pacman.BoardPositions ... – PowerPoint PPT presentation

Number of Views:15
Avg rating:3.0/5.0
Slides: 19
Provided by: cseBu
Category:
Tags: collections | man | pac

less

Transcript and Presenter's Notes

Title: Collections


1
Collections Definite Loops
  • CSE 115
  • Spring 2006
  • April 10, 12, 14 2006

2
Discussion of PacMan
  • Make note of the requirements listed in the lab
    description.
  • Attend recitations for additional advice and
    assistance.
  • In class, we will build a game (Tic Tac Toe).

3
Collections
  • Storage for many objects.
  • Two main types
  • Bags
  • Associations
  • We will discuss use of collections, you will see
    how to write your own collection classes in CSE
    116 250.

4
Java Collections Framework
  • Java provides implementations for a number of
    standard collections classes in the java.util
    package.
  • The root interface of the collections hierarchy
    is Collection.

5
The CollectionltEgt interface
  • Note the ltEgt after the word collection.
  • The ltEgt indicates that this class can use a
    generic type (parameterized type).
  • When you create an instance of a class with a
    generic type, you specify in the ltgt the actual
    type for the generic.

6
Using Generic Types
  • For collections, what you are specifying with the
    generic type is the type of objects you will be
    storing in a collection.
  • Eg) I want a bag of cats.
  • When you do this, Java ensures that only objects
    of the type specified go in and you can be
    assured that only objects of that type come out.

7
A usable bag
  • java.util.ArrayListltEgt
  • Note the operations that you can perform on this
    collection.
  • The most important will be creating an instance
    of the collection, inserting elements, removing
    elements, and finding if elements are in the
    collection.

8
Another important collection operation
  • Iterating over all the elements of a collection
    and performing some operation with/on each
    element of the collection.
  • This process can be accomplished by using a
    special object provided by Java called an
    iterator.
  • In Java 5, the use of the iterator has been
    replaced with the for-each loop.

9
For-each loop
  • Allows access to each element of a collection.
  • Syntax
  • for(TypeOfElementInCollection giveNameToElement
    NameOfCollection)
  • //write code for what to do with each
    //element.

10
ArrayLists and PacMan
  • Note that in the CSE115.Pacman.BoardPositions
    class there are ArrayLists for each of the type
    of cells on the PacMan board. Further
    explanation will be provided in recitation.

11
A useful association
  • java.util.HashMapltK, Vgt
  • Associates a key with a value.
  • Both the key and value are objects.
  • User specifies what type of key and value is used
    when HashMap is created.

12
Useful operations with HashMaps
  • put (put a key/value pair in the HashMap)
  • Remove
  • Look up a value using its key
  • Iterating using the for-each loop

13
Using for-each with HashMaps (note .values())
  • java.util.HashMapltPosition, Cellgt _board new
    java.util.HashMap ltPosition, Cellgt()
  • //magic happens to put things into board.
  • for(Cell c _board.values()
  • c.draw()

14
The keyword for
  • The for-each is a specialized loop designed to
    work with collections.
  • for is a keyword in Java that tells us there is a
    loop.
  • You can create a regular for-loop for use in
    your programs.

15
Loops (Iteration/Repetition)
  • The ability to do a task repeatedly.
  • The functionality of repetition is most often
    implemented in programming languages using loops.

16
Definite Loop
  • The for-loop is characterized as a definite
    loop and is normally used when you know how many
    times you want a specific task to be performed.
    It is sometimes referred to as a counting loop.

17
Entry Test Loop
  • A for-loop is also characterized as an
    entry-test loop. That is, a condition about
    whether the loop should continue is tested before
    actually doing the work of the loop.

18
Syntax of for-loop
  • for (initialization condition increment)
  • //loop body
  • Usually, the initialization is of a loop counter
    variable that is checked against a bounds in the
    condition and is incremented in the increment
    step.
Write a Comment
User Comments (0)
About PowerShow.com