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

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
COMP 121 Week 9: AbstractList and ArrayList. Objectives List common operations and properties of Lists as distinct from Collections Extend the AbstractCollection.
Generics and the ArrayList Class
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.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Using Collections. Review of Collections Using an ArrayList It increases its capacity as necessary. It keeps a private count ( size() accessor). It keeps.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Using ArrayList. Lecture Objectives To understand the foundations behind the ArrayList class Explore some of the methods of the ArrayList class.
Lists and the Collection Interface Chapter 4. Chapter 4: Lists and the Collection Interface2 Chapter Objectives To become familiar with the List interface.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
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.
Introduction to Classes and Objects CS-2303, C-Term Introduction to Classes and Objects CS-2303 System Programming Concepts (Slides include materials.
Modul 3 Collections af objekter Arraylist Collections Objektorienteret design og Java. 4.0.
Programming with Collections Grouping & Looping - Collections and Iteration Week 7.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Random, Collections & Loops Chapter 5 Copyright © 2012 Pearson Education, Inc.
Programming With Java ICS201 University Of Ha’il1 Chapter 14 Generics and The ArrayList Class.
Grouping objects Collections and iterators. Main concepts to be covered Collections Loops Iterators.
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.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
Grouping objects Introduction to collections 5.0.
1 CSC 221: Computer Programming I Fall 2004 Lists, data access, and searching  ArrayList class  ArrayList methods: add, get, size, remove  example:
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)
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
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.
CSE 1201 Object Oriented Programming ArrayList 1.
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.
Written by: Dr. JJ Shepherd
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.
Java How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Collections and Iteration Week 13.  Collections  ArrayList objects  Using loops with collections Collections and Iteration CONCEPTS COVERED THIS WEEK.
Arrays (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
19-Mar-16 Collections and ArrayLists.. 2 Collections Why use Collections. Collections and Object-Orientation. ArrayLists. Special Features. Creating ArrayLists.
(C) 2010 Pearson Education, Inc. All rights reserved. Java How to Program, 8/e.
Grouping objects Introduction to collections 6.0.
Chapter 8: Understanding Collections Textbook: Chapter 4.
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.
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 Introduction to collections
Coding #1 if(!(index < 0 || index >= list.size( )-1))
COS 260 DAY 7 Tony Gauvin.
Objects First with Java Introduction to collections
Collections & Definite Loops
COS 260 DAY 8 Tony Gauvin.
COS 260 DAY 8 Tony Gauvin.
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Object Oriented Programming in java
Objects First with Java Introduction to collections
Collections and iterators
Collections and iterators
Classes and Objects Systems Programming.
Arrays.
Presentation transcript:

GROUPING OBJECTS CITS1001

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

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

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 4

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

Collections We specify The type of the collection: ArrayList The type of the objects it will contain: private ArrayList files; We say “ArrayList of String” 6

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 etc. 7

Creating an ArrayList object In versions of Java prior to version 7 files = new ArrayList (); Java 7 introduced ‘diamond notation’ files = new ArrayList<>(); The type parameter can be inferred from the variable being assigned to A convenience 8

Object structures with collections 9

Adding a third file 10

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? 11

Using the collection public class MusicOrganizer { private ArrayList 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) 12

Index numbering 13

Retrieving an object Index validity checks 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. } Retrieve and print the file name Needed? (Error message?) 14

Removal may affect numbering 15

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

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 17

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 18

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 19

For-each loop pseudo code for(ElementType element : collection) { loop body } For each element in collection, do the things in the loop body. loop header for keyword Statement(s) to be repeated Pseudo-code expression of the actions of a for-each loop General form of the for-each loop 20

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 21

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

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 } 23

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’ 24

Process all items pattern The for-each loop is used whenever we need to perform some action on every item in a collection: view every one change every one check every one select some or count some from every one Examples: see MusicOrganiser.java and Club.java code in the handouts and MarksAnalyser in the labs 25

Summary of for-each rule 1. Get first element of the collection files 2. Execute statement using that value 3. Get next element from the collection and repeat from 2 4. But if no elements left then stop for (String filename : files) { if (filename.contains(searchString)) { System.out.println(filename); } } 26