Java Collections: - PowerPoint PPT Presentation

1 / 10
About This Presentation
Title:

Java Collections:

Description:

When you lookup my phone number in the directory you don't have to worry that my ... For example, a phone directory would contain phone number values (probably ... – PowerPoint PPT presentation

Number of Views:49
Avg rating:3.0/5.0
Slides: 11
Provided by: csU57
Category:

less

Transcript and Presenter's Notes

Title: Java Collections:


1
Session 9
  • Java Collections
  • ArrayList
  • HashMap

2
Remember last semester
  • One of the last assignments you did was a
    SlideShow class.
  • This class read from a file
  • The number of pictures
  • The filenames for those pictures
  • The filename for a Sound file

3
Remember last semester
  • One of the last assignments you did was a
    SlideShow class.
  • This class read from a file
  • The number of pictures WHY???
  • The filenames for those pictures
  • The filename for a Sound file

4
Using Arrays
  • Arrays are fixed size collections of some
    predefined data type.
  • Picture slides new Picture5
  • slides0 new Picture(picture1.jpg)
  • slides1 new Picture(picture2.jpg)
  • System.out.println(size slides.length)
  • Picture p slides1

5
Using Arrays
  • But sometimes you dont know the size when you
    start.
  • Picture slides new Picture???
  • And what happens when you find one more thing
    that you want to put in the array?
  • slides4 new Slide(picture5.jpg)
  • ???? new Slide(picture6.jpg)

6
ArrayList
  • ArrayLists are flexible sized collections of some
    predefined, referential data type.
  • import java.util.ArrayList
  • ArrayList slides new
    ArrayList()
  • slides.add(new Picture(picture1.jpg))
  • slides.add(new Picture(picture2.jpg))
  • System.out.println(size slides.size())
  • Picture p slides.get(1)

7
Another Collection type
  • With Arrays and ArrayLists you look up each
    object by its number.
  • What can you do when you dont want to use
    numbers?
  • When you lookup my phone number in the directory
    you dont have to worry that my number is the
    3487th number in the book
  • Not to mention, you dont want to worry that my
    ranking will change when Dr. Jones retires
    (moving me to 3486.

8
HashMaps
  • HashMaps are flexible sized collections of some
    predefined, referential data type.
  • They are accessed using some predefined key data
    type.
  • For example, a phone directory would contain
    phone number values (probably stored as Strings)
    referenced by some name key (also a String).

9
HashMaps
  • import java.util.HashMap
  • HashMap directory
  • new HashMap()
  • directory.put(Schafer,3-2187)
  • directory.put(Fienup,3-5918)
  • System.out.println(size directory.size())
  • String phoneNum directory.get(Schafer)

10
Lets actually see this in action
Write a Comment
User Comments (0)
About PowerShow.com