Collections - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Collections

Description:

There are three kinds of collections in Java: Arrays ... James Gosling (author) uses before. Patrick Naughton (author) uses after. 8/20/09. collections ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 19
Provided by: calpoly
Category:

less

Transcript and Presenter's Notes

Title: Collections


1
Collections
  • Robert Stumpf, Professor
  • Computer Information Systems
  • California State Polytechnic University, Pomona

2
Collections
  • There are three kinds of collections in Java
  • Arrays containing data types or objects
  • Ordered Collections containing objects only
  • Dictionaries containing objects only
  • All three collections are objects

3
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

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

5
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

6
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

7
Arrays
  • Other information
  • Length is obtained from a public static 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

8
Ordered Collections
  • In Java an Ordered Collection is called a Vector
  • For example to declare and initialize
  • Vector orderedCollection
  • And initialize
  • orderedCollection new Vector ( )

9
Ordered Collections
  • To add to collection
  • String string new String("Toyota")
  • orderedCollection.add(string)
  • To get from collection
  • String string (String) orderedCollection.
    get(0)
  • Remember an Vector can only hold objects

10
Ordered Collections
  • To locate in collection
  • String string new String("Toyota")
  • if (orderedCollection.contains (string) ) int
    index orderedCollection.indexOf ( string)
  • To find the size of a collection use
  • int size orderedCollection.size ( )
  • Remember a Vector may grow

11
Ordered Collections
  • To iterate through a vectors values
  • Iterator iterator
  • iterator orderedCollection.iterator()
  • while (iterator.hasNext())
  • value (String) iterator.next()
  • System.out.println ( "Value is " value)

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

13
Dictionaries
  • To add to a dictionary
  • dictionary.put ("Sue", "3249")
  • To get from dictionary
  • String string (String) dictionary.get
    ("Sue")
  • Remember a Dictionary can only hold objects
  • Remember a Dictionarys key must be an object

14
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

15
Dictionaries
  • To iterate through a dictionaries keys
  • iterator dictionary.keySet().iterator()
  • while (iterator.hasNext())
  • key (String) iterator.next()
  • System.out.println("Key is " key)

16
Dictionaries
  • To iterate through a dictionaries values
  • iterator dictionary.values().iterator()
  • while (iterator.hasNext())
  • value (String) iterator.next()
  • System.out.println("Value is " value)

17
Summary
  • Remember arrays in Java are objects
  • An Ordered Collection is called a Vector
  • A Dictionary is called a Hash Table
  • Arrays are fixed in size while Ordered
    Collections and Dictionaries may grow
  • Arrays can hold data types while Ordered
    Collections and Dictionaries hold objects

18
Thank You
  • Collections are the workhorses of Java
  • Any questions should be directed to Professor
    Robert Stumpf
  • Email rvstumpf_at_csupomona.edu
  • Web Sitehttp//www.csupomona.edu/rvstumpf
Write a Comment
User Comments (0)
About PowerShow.com