Collections - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

Collections

Description:

phoneBook = new Hashtable ( ); Note the parent class of Hashtable is Dictionary. 15 ... In the phone book example, this is would be the list of people's names ... – PowerPoint PPT presentation

Number of Views:57
Avg rating:3.0/5.0
Slides: 22
Provided by: calpoly
Category:

less

Transcript and Presenter's Notes

Title: Collections


1
Collections
  • Working with More than One Number or Data Type

2
Collections
  • There are three kinds of collections in Java
  • Arrays contain
  • Can contain data types or objects
  • Ordered Collections
  • Called Vectors in Java
  • Contain objects
  • Dictionaries
  • Called Hashtables in Java
  • Both keys and related values must be objects
  • All three collections are themselves objects

3
Which Type of Collection Should We Use?
  • Type depends on the nature of the problem
  • Key characteristics to determine which type of
    collection to use
  • Array easy to create and use, but fixed size.
  • Ordered Collection (or Vector) just about as
    easy to create and use, and can grow and shrink.
    More flexible.
  • Dictionary (or Hashtable) used to store and
    retrieve values based on a key. Fast and easy
    for this purpose. Can also enumerate keys and
    values.

4
Arrays
  • Can hold any data type or object
  • The size is fixed (!!) during declaration
  • For example
  • int daysInMonth
  • String monthNames
  • To initialize
  • daysInMonth new int 12
  • monthNames new String 12

5
Arrays
  • Alternate array declaration
  • For example
  • int daysInMonth
  • String monthNames
  • The choice of placing the brackets before or
    after is a matter of style
  • Garside and James Gosling (author of Java) uses
    before
  • Patrick Naughton (author of Java) uses after

6
Arrays
  • To populate (beginning at zero)
  • int daysInMonth new int 12
  • daysInMonth 0 31
  • daysInMonth 1 28
  • And
  • String monthNames new String 12
  • monthNames 0 January
  • monthNames 1 February

7
Arrays
  • An alternate way to populate
  • int daysInMonth 31, 28, 31, 30, 31, 30,
    31, 31, 30, 31, 10, 31
  • And
  • String monthNames January
    February March, April, May, June,
    July, August, September, October,
    November, December

8
Arrays
  • Other information
  • Length is obtained from a public class variable
    as follows
  • int size1 monthNames.length
  • int size2 daysInMonth.length
  • It is recommended that the programmer always used
    this class variable rather than a constant to
    make maintenance easier

9
Using Loops With Arrays
  • int daysInMonth 31, 28, 31, 30, 31, 30,
    31, 31, 30, 31, 10, 31
  • int sum 0
  • for (int ctr 0 ctr lt 11 ctr)
  • sum sum daysInMonthctr

10
Ordered Collections(for general info only)
  • An Ordered Collection is similar to an array but
    it is more flexible
  • can grow and shrink
  • despite its name, an Ordered Collection does not
    keep its elements in order
  • In Java an Ordered Collection is called a Vector

11
Declaring and Initializing Ordered Collections
  • Declaring a Vector
  • Vector orderedCollection
  • Vector listOfCustomers
  • And initializing it
  • orderedCollection new Vector ( )

12
Declaring and Initializing Ordered Collections
  • To add to ordered collection
  • String thisCarType new String("Toyota")
  • orderedCollection.addElement(thisCarType)
  • To retrieve from the ordered collection
  • String car (String) orderedCollection.elementAt
    (0)
  • Note how you have to cast the Object returned by
    the method elementAt(int) back to the specific
    type of Object, in this case a String, which was
    actually stored there

13
Ordered Collections
  • To locate in collection
  • String carName new String("Toyota")
  • if (orderedCollection.contains (carName) ) int
    index orderedCollection.indexOf (carName)
  • To find the size of a collection use
  • int size orderedCollection.size ( )
  • Remember a Vector may grow
  • Now might be a good time to look at the API for
    the Vector class

14
Dictionaries
  • In Java an Dictionary is called a Hashtable
  • For example to declare and initialize
  • Hashtable dictionary
  • Hashtable phoneBook
  • And initialize
  • dictionary new Hashtable ( )
  • phoneBook new Hashtable ( )
  • Note the parent class of Hashtable is Dictionary

15
Dictionaries
  • To add to a dictionary
  • dictionary.put ("Sue", "3249")
  • To get from dictionary
  • String string (String) dictionary.get ("Sue")
  • Note 1 what you store is what you retrieve. If
    you store a Person object, then you retrieve a
    Person object, not a String which is the name of
    the person, or an int which is their age, or
  • Note 2 an object of type Object is returned, so
    you have to "cast" that Object into a Person
    object (or into whatever type of object was
    originally stored).

16
Dictionaries
  • To find the size of a dictionary use
  • int size dictionary.size ( )
  • To check if key is not there
  • String string (String) dictionary.get ("Sue")
  • if (string null)
  • System.out.println ( "Not found " string)
  • Remember a Dictionary may grow

17
Dictionaries
  • Sometimes we want to go through all the keys in
    the dictionary
  • In the phone book example, this is would be the
    list of people's names
  • Sometimes we want to go through all the values in
    the dictionary
  • In the phone book example, this would be a list
    of all the phone numbers
  • This is called "enumeration", and the list of
    keys (or values) is also called "an enumeration"

18
Dictionaries
  • To enumerate through a dictionary's keys
  • Enumeration enumeration
  • enumeration dictionary.keys ( )
  • while(enumeration.hasMoreElements ( ) )
  • key (String) enumeration.nextElement ( )
  • System.out.println ( "Key is " key)

19
Dictionaries
  • To enumerate through a dictionary's values
  • Enumeration enumeration
  • enumeration dictionary.elements ( )
  • while(enumeration.hasMoreElements ( ) )
  • value (String) enumeration.nextElement (
    )
  • System.out.println ( "Value is "
    value)
  • Since you retrieve an Object, you need to cast it
    back to the type of object it really is (for
    example a Car or Person object)
  • you then pull out the values from the object
    using your getters

20
Which Type of Collection Should We Use?
  • Type depends on the nature of the problem
  • Key characteristics to determine which type of
    collection to use
  • Array easy to create and use, but fixed size.
  • Ordered Collection (or Vector) just about as
    easy to create and use, and can grow and shrink.
    More flexible.
  • Dictionary (or Hashtable) used to store and
    retrieve values based on a key. Fast and easy
    for this purpose. Can also enumerate keys and
    values.

21
Summary
  • Remember Arrays in Java are objects
  • All general purpose computer languages have
    arrays, and you need to be able to program using
    them
  • An Ordered Collection is called a Vector
  • A Dictionary is called a Hashtable
  • Arrays are fixed in size
  • Arrays can not contain a mix of things
  • Arrays can be specified to contain data types or
    Objects
  • Vectors and Dictionaries may grow in size
  • Vectors and Dictionaries can not store data
    types, only Objects
Write a Comment
User Comments (0)
About PowerShow.com