Java Collections and the ArrayList class - PowerPoint PPT Presentation

1 / 4
About This Presentation
Title:

Java Collections and the ArrayList class

Description:

you need to iterate over the elements, and add and remove elements ... which means dynamically decreasing the size of the array ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 5
Provided by: Steve57
Category:

less

Transcript and Presenter's Notes

Title: Java Collections and the ArrayList class


1
Java Collections and the ArrayList class
  • What are Collections?
  • Classes provided (in the java.util package) for
    managing collections of Objects
  • Main types of collections
  • Sets the set of people in this class
  • no duplicates
  • no inherent order
  • no ith element
  • you need to iterate over the elements, and add
    and remove elements
  • no fixed size to the collection
  • Lists phone messages to return
  • might be duplicates
  • entries are in order (perhaps in the order
    received)
  • you can add to the end, or to the beginning, or
    to the middle
  • you can access the ith element
  • no fixed size to the collection
  • Maps mapping of userids to passwords
  • no duplicate keys
  • one value per key
  • need to access the value for a key quickly don't
    typically traverse the collection

2
The Array "Interface"
  • What can you do with arrays?
  • construct one of a fixed type and length
  • get the ith element
  • set the ith element
  • "fake" a variable-size array with a maximum size
    and "last" pointer
  • which is cumbersome because the "last" pointer
    has to be an additional parameter
  • and eventually you might exceed the maximum and
    then you're truly stuck
  • Additional things that would be useful
  • increase and decrease the collection's size on
    demand
  • add new elements to the array (to the ith
    position)
  • shifting all other elements to the "right"
  • which means dynamically increasing the size of
    the array
  • remove elements from the array (from the ith
    position)
  • shifting all other elements to the "left"
  • which means dynamically decreasing the size of
    the array
  • store objects of different data types in the
    array
  • possibly useful, but less common
  • make the array "non-primitive" so we dont have
    to have special syntax

3
List Collections
  • List is actually an interface, and there are
    several classes that implement it
  • ArrayList is one of them, and the one most
    commonly used in practice
  • The List interface (part of it)
  • add(int index, Object element)
  • add the object at this index, shifting existing
    elements to the right
  • add(Object element)
  • add the object to the end of the list the same
    as add(size(), element)
  • Object remove(int index)
  • remove the object at this index, shifting
    remaining elements to the left
  • boolean remove(Object element)
  • remove the first occurrence of this object,
    shifting remaining elements to the left
  • Object get(int index)
  • Object set(int index, Object element)
  • boolean contains(Object element)
  • int indexOf(Object element)
  • returns the index of the first occurrence of this
    object or -1 if it does not appear in the list
  • Iterator iterator()
  • returns an Iterator that allows sequential access
    through the list
  • (all methods that do comparisons use the equals()
    method)

4
Sample Application Course class
  • A Course has a name (string) and a collection of
    Students
  • A Student has a first name, a last name and a
    year (1, 2, 3, 4)
  • Public interface
  • new Course(String name)
  • String getName()
  • void addStudent(Student aStudent)
  • void removeStudent(Student aStudent)
  • int numStudents()
  • Iterator getStudents()
  • void printClassList()
  • print the class list to the console, in
    alphabetical order
  • Application class
  • (add some students to the course)
  • print list of names of the students in the course
  • for year 1, 2, 3, 4 report the percentage of
    students in that year
Write a Comment
User Comments (0)
About PowerShow.com