grouping objects cits1001. lecture outline the arraylist collection process all items: the for-each...

26
GROUPING OBJECTS CITS1001

Upload: oscar-peters

Post on 08-Jan-2018

219 views

Category:

Documents


0 download

DESCRIPTION

The requirement to group objects Many applications involve collections of objects Personal organizers Library catalogs Student-record system etc. Entries must be accessed efficiently The number of items to be stored varies Need the ability to add and delete items 3

TRANSCRIPT

Page 1: GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2

GROUPING OBJECTSCITS1001

Page 2: GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2

2

Lecture outline• The ArrayList collection• Process all items: the for-each loop

Page 3: GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2

3

The requirement to group objects• Many applications involve collections of objects

• Personal organizers• Library catalogs• Student-record system• etc.

• Entries must be accessed efficiently • The number of items to be stored varies

• Need the ability to add and delete items

Page 4: GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2

4

An organizer for music files• Track files may be added and deleted• There is no pre-defined limit to the number of files• It will tell how many file names are stored in the collection• It will list individual file names

• Explore the music-organizer-v1 project• Grouping objects is a recurring requirement

• The java.util package contains classes for doing this

Page 5: GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2

5

import java.util.ArrayList;

/** * ... */public class MusicOrganizer{ // Storage for an arbitrary number of file names. private ArrayList<String> files;  /** * Perform any initialization required for the * organizer. */ public MusicOrganizer() { files = new ArrayList<String>(); }

...}

Page 6: GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2

6

Collections• We specify

• The type of the collection: ArrayList• The type of the objects it will contain: <String>• private ArrayList<String> files;

• We say “ArrayList of String”

Page 7: GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2

7

Generic classes• Collections are known as parameterized or generic types

• ArrayList implements list functionality:• add, remove, size, get, etc.

• The type parameter says what we want a list of:• ArrayList<Person>• ArrayList<TicketMachine>• etc.

Page 8: GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2

8

Creating an ArrayList object• In versions of Java prior to version 7

• files = new ArrayList<String>();• Java 7 introduced ‘diamond notation’

• files = new ArrayList<>();• The type parameter can be inferred from the variable

being assigned to• A convenience

Page 9: GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2

9

Object structures with collections

Page 10: GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2

10

Adding a third file

Page 11: GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2

11

Features of the collection• It increases its capacity as necessary• It keeps a private count:

• With the size() accessor• It keeps the objects in order• Details of how all this is done are hidden

• Does that matter? • Does not knowing how it works prevent us from using it?

Page 12: GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2

12

Using the collectionpublic class MusicOrganizer{ private ArrayList<String> files;

...  public void addFile(String filename) { files.add(filename); }  public int getNumberOfFiles() { return files.size(); }

...}

Adding a new file

Returning the number of files(delegation)

Page 13: GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2

13

Index numbering

Page 14: GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2

14

Retrieving an object

Index validity checkspublic void listFile(int index){ if(index >= 0 && index < files.size()) { String filename = files.get(index); System.out.println(filename); } else { // This is not a valid index. }} Retrieve and print the file name

Needed? (Error message?)

Page 15: GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2

15

Removal may affect numbering

Page 16: GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2

16

The general utility of indices• Using integers to index collections has a general utility:

• ‘next’ is index + 1• ‘previous’ is index – 1• ‘last’ is list.size() – 1• ‘the first three’ is the items at indices 0, 1, 2

• We could also think about accessing items in sequence: 0, 1, 2, …

Page 17: GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2

17

Review• Collections allow an arbitrary number of objects to be stored

• Class libraries usually contain tried-and-tested collection classes

• Java’s class libraries are called packages• We have used the ArrayList class from the java.util package

Page 18: GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2

18

Review• 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

• ArrayList is a parameterized or generic type

Page 19: GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2

19

Iteration fundamentals• We often want to repeat some actions over and over

• e.g. “do this action for each student in the university”• e.g. “do this action seventeen times”• e.g. “do this action until this condition is true”

• Java loops provide us with a way to control how many times we repeat these actions

• With collections, we often want to repeat things once for every object in a particular collection

Page 20: GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2

20

For-each loop pseudo code

for(ElementType element : collection) { loop body}

For each element in collection, do the things in the loop body.

loop headerfor keyword

Statement(s) to be repeated

Pseudo-code expression of the

actions of a for-each loop

General form of the for-each loop

Page 21: GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2

21

A Java example

/** * List all file names in the organizer. */public void listAllFiles(){ for(String filename : files) { System.out.println(filename); }}

for each filename in files, print out filename

Page 22: GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2

22

Selective processing• Statements can be nested, giving greater selectivity:

public void findFiles(String searchString){ for(String filename : files) { if(filename.contains(searchString)) { System.out.println(filename); } }}

Page 23: GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2

23

Search and return pattern

public String findFiles(String searchString){ for (String filename : files) { if (filename.contains(searchString)) { return filename; // return the first match if one is found } } return “”; //return empty string if NO match is found}

Page 24: GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2

24

Critique of for-each• Easy to write• Termination happens naturally• The collection cannot be changed• There is no index provided

• Not all collections are index-based• We can’t stop part way through

• Except when using a return statement• It provides ‘definite iteration’, aka ‘bounded iteration’

Page 25: GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2

25

Process all items patternThe for-each loop is used whenever we need to perform some action on every item in a collection:

view every onechange every onecheck every oneselect some or count some from every one

Examples: see MusicOrganiser.java and Club.java code in the handouts and MarksAnalyser in the labs

Page 26: GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2

26

Summary of for-each rule1. Get first element of the collection files2. Execute statement using that value3. Get next element from the collection and repeat from 24. But if no elements left then stop

for (String filename : files) { if (filename.contains(searchString)) { System.out.println(filename); }}