grouping objects l.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Grouping objects PowerPoint Presentation
Download Presentation
Grouping objects

Loading in 2 Seconds...

play fullscreen
1 / 21

Grouping objects - PowerPoint PPT Presentation


  • 138 Views
  • Uploaded on

Grouping objects. Arrays, Collections and Iterators. 1.0. Main concepts to be covered. Arrays Collections Iterators. Requirement to group objects. Many applications for collections of objects: Personal organizers Library catalogs Student-record system

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Grouping objects' - nerina


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
grouping objects

Grouping objects

Arrays, Collections and Iterators

1.0

main concepts to be covered
Main concepts to be covered
  • Arrays
  • Collections
  • Iterators
requirement to group objects
Requirement to group objects
  • Many applications for collections of objects:
    • Personal organizers
    • Library catalogs
    • Student-record system
  • The number of items to be stored varies:
    • Items added
    • Items deleted
fixed size collections
Fixed-size collections
  • Programming languages usually offer a special fixed-size collection type: an array
  • Arrays are built-in, use [] syntax
  • Java arrays can store objects or primitive-type values
  • Maximum collection size must be fixed at Array creation time
  • How is Array creation time more dynamic than in other programming languages, such as C++?
creating an array object
Creating an array object

public class LogAnalyzer

{

private int[] hourCounts;

private LogfileReader reader;

public LogAnalyzer()

{

hourCounts = new int[24];

reader = new LogfileReader();

}

...

}

Array variable declaration

Array object creation

using an array
Using an array
  • Square-bracket notation is used to access an array element:hourCounts[hour]
  • Elements are used like ordinary variables
    • In an expression:
      • adjusted = hourCounts[hour] – 3;
      • hourCounts[hour]++;
class libraries
Class libraries
  • Collections of useful classes
  • Encourages reuse of design and code
  • Java organizes its libraries in packages
  • The java.util package includes classes for grouping objects in collections
a personal notebook
A personal notebook
  • Notes may be stored
  • No limit to the number of notes
  • It tells how many notes are stored
slide10

import java.util.ArrayList;

/**

* ...

*/

public class Notebook

{

// Storage for an arbitrary number of notes.

private ArrayList notes;

/**

* Perform any initialization required for the

* notebook.

*/

public Notebook()

{

notes = new ArrayList();

}

...

}

features of arraylist
Features of ArrayList
  • It keeps the objects in order
  • It increases its capacity as necessary
  • It keeps a private count: size() accessor retrieves it
using arraylist collection
Using ArrayList collection

public class Notebook

{

private ArrayList notes;

...

public void storeNote(String note)

{

notes.add(note);

}

public int numberOfNotes()

{

return notes.size();

}

...

}

Adding a new note

Returning the number of notes

(delegation).

adding a third note
Adding a third note

myBook.add(“11:30 meet John”)

retrieving an object
Retrieving an object

public void showNote(int noteNumber)

{

if(noteNumber<0 && noteNumber >= numberOfNotes()) {

// This is not a valid note number.

}

else System.out.println(notes.get(noteNumber));

}

Retrieve and print the note

review arraylist
Review: ArrayList
  • Items may be added and removed
  • Each item has an index
  • Index values may change if items are removed or further items added
  • The main ArrayList methods are add, get, remove and size
  • For more methods, see API document
collections framework
Collections framework
  • Goals comparable to C++’s STL
  • Data structures:
    • Vector, LinkedList, HashSet, etc.
  • Algorithms:
    • Sorting, searching, shuffling, etc.
loops and bugs
Loops and bugs
  • Why are loops often a cause of bugs?
  • What kind of bugs?
  • Wouldn’t be nice to avoid these bugs?
iterating over a collection
Iterating over a collection

Returns an Iterator object

java.util.Iterator

Iterator it = myCollection.iterator();

while(it.hasNext()) {

call it.next() to get the next object

do something with that object

}

public void listNotes()

{

Iterator it = notes.iterator();

while(it.hasNext()) {

System.out.println(it.next());

}

}

How does Iteratorhelp avoid bugs?

review loops and iterator
Review: loops and Iterator
  • Java while and for loops are similar to C++
    • Similar bugs, too!
  • Collection classes have special Iterator objects that simplify iteration over the whole collection
    • Bounds-checking methods help avoid bugs