Objects First with Java Introduction to collections

Slides:



Advertisements
Similar presentations
Grouping objects Iterators. Iterator and iterator() Collections have an iterator() method. This returns an Iterator object. Iterator has three methods:
Advertisements

Grouping objects Arrays and for loops. Fixed-size collections Sometimes the maximum collection size can be pre-determined. Programming languages usually.
Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.
1 Features of the collection It increases its capacity as necessary. It keeps a private count: –size() accessor. It keeps the objects in order. Details.
Grouping objects Introduction to collections 5.0.
Grouping Objects Arrays and for loops. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Fixed-Size Collections.
Using Collections. Review of Collections Using an ArrayList It increases its capacity as necessary. It keeps a private count ( size() accessor). It keeps.
Grouping Objects 3 Iterators, collections and the while loop.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Grouping Objects 2 Collections and the for-each loop Collections and the while loop.
Grouping Objects 1 Introduction to Collections.
Grouping objects Collections and iterators. 04/11/2004Lecture 4: Grouping Objects2 Main concepts to be covered Collections Loops Iterators Arrays.
More sophisticated behavior Using library classes to implement some more advanced functionality 3.0.
Modul 3 Collections af objekter Arraylist Collections Objektorienteret design og Java. 4.0.
Programming with Collections Grouping & Looping - Collections and Iteration Week 7.
Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.
Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
REPETITION CITS1001. Scope of this lecture Repetition for loops while loops 2.
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 2.0.
Grouping objects Arrays, Collections and Iterators 1.0.
Grouping objects Introduction to collections 5.0.
Grouping objects Collections and iterators Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
JAVA COLLECTIONS M. TAIMOOR KHAN (ADAPTED FROM SWINBURNE NOTES)
Grouping objects Iterators. Iterator type Third variation to iterate over a collection Uses a while loop and Iterator object But NO integer index variable.
Objects First With Java A Practical Introduction Using BlueJ Grouping objects Collections and iterators 1.0.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
GROUPING OBJECTS CITS1001. Lecture outline The ArrayList collection Process all items: the for-each loop 2.
Grouping objects Arrays. 2 Fixed-size collections The maximum collection size may be pre-determined with an upper limit Array is an fixed-size collection.
Grouping objects Introduction to collections 5.0.
1 COS 260 DAY 7 Tony Gauvin. 2 Agenda Questions? 3 rd Mini quiz next class (September 28) –Chap 3 concepts Assignment 1 Corrected Assignment 2 posted.
1 COS 260 DAY 9 Tony Gauvin. 2 Agenda Questions? 3 rd Mini quiz –Good results ->All A’s –Threw out question on name overloading 4 th Mini quiz next class.
Collections and Iteration Week 13.  Collections  ArrayList objects  Using loops with collections Collections and Iteration CONCEPTS COVERED THIS WEEK.
Grouping objects Iterators, collections and the while loop Equality and Equality == and equals(…)
Fixed-sized collections Introduction to arrays 6.0.
Grouping objects Introduction to collections 6.0.
Coming up ArrayList ArrayList vs Array – Declaration – Insertion – Access – Removal Wrapper classes Iterator object.
Chapter 8: Understanding Collections Textbook: Chapter 4.
Arrays Chapter 7.
1 © 2017 Pearson Education, Inc. Hoboken, NJ. All rights reserved. for-each PROS easy to use access to ALL items one-by-one ability to change the state.
Chapter VII: Arrays.
for-each PROS CONS easy to use access to ALL items one-by-one
Objects First with Java CITS1001 week 4
Objects First with Java CITS1001
Objects First with Java Introduction to collections
Fixed-sized collections
ADT’s, Collections/Generics and Iterators
Chapter 7 Part 1 Edited by JJ Shepherd
Chapter 5: Control Structures II
Loop Structures.
COS 260 DAY 7 Tony Gauvin.
Object INTERACTION CITS1001 week 3.
Functional Processing of Collections (Advanced)
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
COS 260 DAY 9 Tony Gauvin.
More sophisticated behavior
4 Grouping objects (cont.)
Objects First with Java Introduction to collections
Arrays, For loop While loop Do while loop
COS 260 DAY 8 Tony Gauvin.
COS 260 DAY 8 Tony Gauvin.
Object Oriented Programming in java
Java Programming Control Structures Part 1
COS 260 DAY 11 Tony Gauvin.
Collections and iterators
COS 260 DAY 4 Tony Gauvin.
Review: libraries and packages
Loops and Iteration CS 21a: Introduction to Computing I
Collections and iterators
Presentation transcript:

Objects First with Java Introduction to collections Grouping objects Introduction to collections 5.0 © David J. Barnes and Michael Kölling

Main concepts to be covered Objects First with Java Main concepts to be covered Collections (especially ArrayList) Builds on the abstraction theme from the last chapter. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

The requirement to group objects Objects First with Java The requirement to group objects Many applications involve collections of objects: Personal organizers. Library catalogs. Student-record system. The number of items to be stored varies. Items added. Items deleted. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

An organizer for music files Objects First with Java An organizer for music files Track files may be added. 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. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java Class libraries Collections of useful classes. We don’t have to write everything from scratch. Java calls its libraries, packages. Grouping objects is a recurring requirement. The java.util package contains classes for doing this. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java 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>(); } ... Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java Collections We specify: the type of collection: ArrayList the type of objects it will contain: <String> private ArrayList<String> files; We say, “ArrayList of String”. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java Generic classes Collections are known as parameterized or generic types. ArrayList implements list functionality: add, get, size, etc. The type parameter says what we want a list of: ArrayList<Person> ArrayList<TicketMachine> etc. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

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.

Object structures with collections Objects First with Java Object structures with collections Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java Adding a third file Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Features of the collection Objects First with Java Features of the collection It increases its capacity as necessary. It keeps a private count: size() accessor. It keeps the objects in order. Details of how all this is done are hidden. Does that matter? Does not knowing how prevent us from using it? Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java Using the collection public 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) Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java Index numbering Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java Retrieving an object public 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. Index validity checks Retrieve and print the file name Needed? (Error message?) Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Removal may affect numbering Objects First with Java Removal may affect numbering Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

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, …

Objects First with Java 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. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java 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. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Interlude: Some popular errors... Objects First with Java Interlude: Some popular errors... Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java What’s wrong here? /** * Print out info (number of entries). */ public void showStatus() { if(files.size() == 0); { System.out.println("Organizer is empty"); } else { System.out.print("Organizer holds "); System.out.println(files.size() + " files"); } } Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java This is the same as before! /** * Print out info (number of entries). */ public void showStatus() { if(files.size() == 0); { System.out.println("Organizer is empty"); } else { System.out.print("Organizer holds "); System.out.println(files.size() + "files"); } } Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java This is the same again /** * Print out info (number of entries). */ public void showStatus() { if(files.size() == 0) ; { System.out.println("Organizer is empty"); } else { System.out.print("Organizer holds "); System.out.println(files.size() + "files"); } } Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java and the same again… /** * Print out info (number of entries). */ public void showStatus() { if(files.size() == 0) { ; } { System.out.println("Organizer is empty"); } else { System.out.print("Organizer holds "); System.out.println(files.size() + "files"); } } Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java This time I have a boolean field called ‘isEmpty’... What’s wrong here? /** * Print out info (number of entries). */ public void showStatus() { if(isEmpty = true) { System.out.println("Organizer is empty"); } else { System.out.print("Organizer holds "); System.out.println(files.size() + "files"); } } Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java This time I have a boolean field called ‘isEmpty’... The correct version /** * Print out info (number of entries). */ public void showStatus() { if(isEmpty == true) { System.out.println("Organizer is empty"); } else { System.out.print("Organizer holds "); System.out.println(files.size() + "files"); } } Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java What’s wrong here? /** * Store a new file in the organizer. If the * organizer is full, save it and start a new one. */ public void addFile(String filename) { if(files.size() == 100) files.save(); // starting new list files = new ArrayList<String>(); files.add(filename); } Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java This is the same. /** * Store a new file in the organizer. If the * organizer is full, save it and start a new one. */ public void addFile(String filename) { if(files.size == 100) files.save(); // starting new list files = new ArrayList<String>(); files.add(filename); } Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java The correct version /** * Store a new file in the organizer. If the * organizer is full, save it and start a new one. */ public void addFile(String filename) { if(files.size == 100) { files.save(); // starting new list files = new ArrayList<String>(); } files.add(filename); } Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java Collections and the for-each loop Grouping objects Collections and the for-each loop © David J. Barnes and Michael Kölling

Main concepts to be covered Objects First with Java Main concepts to be covered Collections Loops: the for-each loop Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java Iteration We often want to perform some actions an arbitrary number of times. E.g., print all the file names in the organizer. How many are there? Most programming languages include loop statements to make this possible. Java has several sorts of loop statement. We will start with its for-each loop. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Iteration fundamentals Objects First with Java Iteration fundamentals We often want to repeat some actions over and over. Loops provide us with a way to control how many times we repeat those actions. With collections, we often want to repeat things once for every object in a particular collection. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

For-each loop pseudo code Objects First with Java For-each loop pseudo code General form of the for-each loop for keyword loop header for(ElementType element : collection) { loop body } Statement(s) to be repeated Pseudo-code expression of the actions of a for-each loop For each element in collection, do the things in the loop body. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java 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 Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java Review Loop statements allow a block of statements to be repeated. The for-each loop allows iteration over a whole collection. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

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); } }

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; e.g. find-the-first-that-matches. It provides ‘definite iteration’ – aka ‘bounded iteration’.

Objects First with Java Indefinite iteration - the while loop Grouping objects Indefinite iteration - the while loop © David J. Barnes and Michael Kölling

Main concepts to be covered Objects First with Java Main concepts to be covered The difference between definite and indefinite (unbounded) iteration. The while loop Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Search tasks are indefinite We cannot predict, in advance, how many places we will have to look. Although, there may well be an absolute limit – i.e., checking every possible location. ‘Infinite loops’ are also possible. Through error or the nature of the task.

Objects First with Java The while loop A for-each loop repeats the loop body for each object in a collection. Sometimes we require more variation than this. We use a boolean condition to decide whether or not to keep going. A while loop provides this control. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java While loop pseudo code General form of a while loop while keyword boolean test while(loop condition) { loop body } Statements to be repeated Pseudo-code expression of the actions of a while loop while we wish to continue, do the things in the loop body Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Looking for your keys Or: while(the keys are missing) { look in the next place; } Or: while(not (the keys have been found)) {

Suppose we don’t find them? Looking for your keys boolean searching = true; while(searching) { if(they are in the next place) { searching = false; } Suppose we don’t find them?

Objects First with Java A Java example /** * List all file names in the organizer. */ public void listAllFiles() { int index = 0; while(index < files.size()) { String filename = files.get(index); System.out.println(filename); index++; } Increment index by 1 while the value of index is less than the size of the collection, get and print the next file name, and then increment index Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Elements of the loop We have declared an index variable. The condition must be expressed correctly. We have to fetch each element. The index variable must be incremented explicitly.

Objects First with Java for-each versus while for-each: easier to write. safer: it is guaranteed to stop. while: we don’t have to process the whole collection. doesn’t even have to be used with a collection. take care: could be an infinite loop. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Searching a collection A fundamental activity. Applicable beyond collections. Necessarily indefinite. We must code for both success and failure – exhausted search. Both must make the loop’s condition false. The collection might be empty.

Finishing a search How do we finish a search? Either there are no more items to check: index >= files.size() Or the item has been found: found == true found ! searching

Continuing a search With a while loop we need to state the condition for continuing: So the loop’s condition will be the opposite of that for finishing: index < files.size() && ! found index < files.size() && searching NB: ‘or’ becomes ‘and’ when inverting everything.

Searching a collection Objects First with Java Searching a collection int index = 0; boolean found = false; while(index < files.size() && !found) { String file = files.get(index); if(file.contains(searchString)) { // We don't need to keep looking. found = true; } else { index++; // Either we found it at index, // or we searched the whole collection. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Indefinite iteration Does the search still work if the collection is empty? Yes! The loop’s body won’t be entered in that case. Important feature of while: The body will be executed zero or more times.

While without a collection Objects First with Java While without a collection // Print all even numbers from 2 to 30. int index = 2; while(index <= 30) { System.out.println(index); index = index + 2; } Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

The String class The String class is defined in the java.lang package. It has some special features that need a little care. In particular, comparison of String objects can be tricky.

Side note: String equality Objects First with Java Side note: String equality tests identity if(input == "bye") { ... } if(input.equals("bye")) { Always use .equals for text equality. tests equality if desired, include more detailed discussion of identity vs equality here. (That's the next 5 slides, up to "Identity vs equality (Strings)". Skip these if this is not needed now. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java Identity vs equality 1 Other (non-String) objects: :Person :Person “Fred” “Jill” == is not true here (of course) person1 person2 person1 == person2 ? Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java Identity vs equality 2 Other (non-String) objects: :Person :Person “Fred” “Fred” == is still not true here (different objects, == tests identity) person1 person2 person1 == person2 ? Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java Identity vs equality 3 Other (non-String) objects: :Person :Person “Fred” “Fred” == is true now (same object) person1 person2 person1 == person2 ? Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Identity vs equality (Strings) Objects First with Java Identity vs equality (Strings) String input = reader.getInput(); if(input == "bye") { ... } == tests identity :String :String == ? "bye" "bye" == is still not true here (different objects, == tests identity) input à (may be) false! Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Identity vs equality (Strings) Objects First with Java Identity vs equality (Strings) String input = reader.getInput(); if(input.equals("bye")) { ... } equals tests equality :String :String ? equals "bye" "bye" == is still not true here (different objects, == tests identity) input à true! Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

The problem with Strings The compiler merges identical String literals in the program code. The result is reference equality for apparently distinct String objects. But this cannot be done for identical strings that arise outside the program’s code; e.g., from user input.

Moving away from String Our collection of String objects for music tracks is limited. No separate identification of artist, title, etc. A Track class with separate fields: artist title filename

Objects First with Java Iterators Grouping objects Iterators also explain: how to pass an ArrayList for testing © David J. Barnes and Michael Kölling

Iterator and iterator() Collections have an iterator() method. This returns an Iterator object. Iterator<E> has three methods: boolean hasNext() E next() void remove()

Using an Iterator object Objects First with Java Using an Iterator object returns an Iterator object java.util.Iterator Iterator<ElementType> it = myCollection.iterator(); while(it.hasNext()) { call it.next() to get the next object do something with that object } public void listAllFiles() { Iterator<Track> it = files.iterator(); while(it.hasNext()) { Track tk = it.next(); System.out.println(tk.getDetails()); } Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java Iterator mechanics Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java myList:List myList.iterator() :Element :Element :Element :Element :Iterator Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java myList:List :Element :Element :Element :Element :Iterator :Iterator ✔ hasNext()? next() Element e = iterator.next(); Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java myList:List :Element :Element :Element :Element :Iterator :Iterator ✔ hasNext()? next() Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java myList:List :Element :Element :Element :Element :Iterator :Iterator ✔ hasNext()? next() Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java myList:List :Element :Element :Element :Element :Iterator :Iterator hasNext()? ✔ next() Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java myList:List :Element :Element :Element :Element :Iterator ✗ hasNext()? Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java Index versus Iterator Ways to iterate over a collection: for-each loop. Use if we want to process every element. while loop. Use if we might want to stop part way through. Use for repetition that doesn't involve a collection. Iterator object. Often used with collections where indexed access is not very efficient, or impossible. Use to remove from a collection. Iteration is an important programming pattern. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Removing from a collection Iterator<Track> it = tracks.iterator(); while(it.hasNext()) { Track t = it.next(); String artist = t.getArtist(); if(artist.equals(artistToRemove)) { it.remove(); } Use the Iterator’s remove method.

Objects First with Java Review Loop statements allow a block of statements to be repeated. The for-each loop allows iteration over a whole collection. The while loop allows the repetition to be controlled by a boolean expression. All collection classes provide special Iterator objects that provide sequential access to a whole collection. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java The auction project The auction project provides further illustration of collections and iteration. Examples of using null. Anonymous objects. Chaining method calls. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

The auction project

null Used with object types. Used to indicate, 'no object'. We can test if an object variable holds the null value: if(highestBid == null) … Used to indicate ‘no bid yet’.

Anonymous objects Objects are often created and handed on elsewhere immediately: Lot furtherLot = new Lot(…); lots.add(furtherLot); We don’t really need furtherLot: lots.add(new Lot(…));

Chaining method calls Methods often return objects. We often immediately call a method on the returned object. Bid bid = lot.getHighestBid(); Person bidder = bid.getBidder(); We can use the anonymous object concept and chain method calls: lot.getHighestBid().getBidder()

Chaining method calls Each method in the chain is called on the object returned from the previous method call in the chain. String name = lot.getHighestBid().getBidder().getName(); Returns a Bid object from the Lot Returns a Person object from the Bid Returns a String object from the Person

Objects First with Java arrays Grouping objects arrays © David J. Barnes and Michael Kölling

Fixed-size collections Objects First with Java Fixed-size collections Sometimes the maximum collection size can be pre-determined. A special fixed-size collection type is available: an array. Unlike the flexible List collections, arrays can store object references or primitive-type values. Arrays use a special syntax. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

The weblog-analyzer project Objects First with Java The weblog-analyzer project Web server records details of each access. Supports analysis tasks: Most popular pages. Busiest periods. How much data is being delivered. Broken references. Analyze accesses by hour. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Creating an array object Objects First with Java Creating an array object Array variable declaration — does not contain size public class LogAnalyzer { private int[] hourCounts; private LogfileReader reader;   public LogAnalyzer() hourCounts = new int[24]; reader = new LogfileReader(); } ... Array object creation — specifies size Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java The hourCounts array Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java Using an array Square-bracket notation is used to access an array element: hourCounts[...] Elements are used like ordinary variables. The target of an assignment: hourCounts[hour] = ...; In an expression: hourCounts[hour]++; adjusted = hourCounts[hour] – 3; Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java Standard array use private int[] hourCounts; private String[] names; ...   hourCounts = new int[24]; hourcounts[i] = 0; hourcounts[i]++; System.out.println(hourcounts[i]); declaration creation use Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java Array literals The size is inferred from the data. private int[] numbers = { 3, 15, 4, 5 }; declaration, creation and initialization Array literals in this form can only be used in declarations. Related uses require new: numbers = new int[] { 3, 15, 4, 5 }; Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java Array length private int[] numbers = { 3, 15, 4, 5 }; int n = numbers.length; no brackets! NB: length is a field rather than a method! It cannot be changed – ‘fixed size’. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java The for loop There are two variations of the for loop, for-each and for. The for loop is often used to iterate a fixed number of times. Often used with a variable that changes a fixed amount on each iteration. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java For loop pseudo-code General form of the for loop for(initialization; condition; post-body action) { statements to be repeated } Equivalent in while-loop form initialization; while(condition) { statements to be repeated post-body action } Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java A Java example for loop version for(int hour = 0; hour < hourCounts.length; hour++) { System.out.println(hour + ": " + hourCounts[hour]); } while loop version int hour = 0; while(hour < hourCounts.length) { System.out.println(hour + ": " + hourCounts[hour]); hour++; } Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java Practice Given an array of numbers, print out all the numbers in the array, using a for loop. int[] numbers = { 4, 1, 22, 9, 14, 3, 9}; for ... Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java Practice Fill an array with the Fibonacci sequence. 0 1 1 2 3 5 8 13 21 34 ... int[] fib = new int[100]; fib[0] = 0; fib[1] = 1; for ... Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

for loop with bigger step Objects First with Java for loop with bigger step // Print multiples of 3 that are below 40. for(int num = 3; num < 40; num = num + 3) { System.out.println(num); } Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling

Objects First with Java Review Arrays are appropriate where a fixed-size collection is required. Arrays use a special syntax. For loops are used when an index variable is required. For loops offer an alternative to while loops when the number of repetitions is known. Used with a regular step size. Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling © David J. Barnes and Michael Kölling