Download presentation
Presentation is loading. Please wait.
1
Grouping Objects 1 Introduction to Collections
3
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Style variable names (should be meaningful) variable names (should be meaningful) comments (tell us information that is not obvious from the code) comments (tell us information that is not obvious from the code) layout (indentation should reflect logical structure) layout (indentation should reflect logical structure) http://www.bluej.org/objects-first/styleguide.html Linked from the “Resources” on the module web page.
4
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Main concepts to be covered Collections (especially: ArrayList)
5
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Requirement to Group Objects Many applications involve collections of objects: Personal organizers Library catalogs Student-record system The number of items stored varies: Items get added Items get deleted
6
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Example: a personal notebook Notes may be stored. Individual notes can be viewed. There is no limit to the number of notes. It can report how many notes are stored. Explore the notebook1 project.
7
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Class Libraries Libraries of useful classes. Libraries of useful classes. We don’t have to write everything from scratch. We don’t have to write everything from scratch. Java calls its libraries, packages. Java calls its libraries, packages. Collections of objects is a recurring requirement. Collections of objects is a recurring requirement. The java.util package contains classes for doing this. The java.util package contains classes for doing this.
8
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling import java.util.ArrayList; /** *... *... */ */ public class Notebook { // Storage for an arbitrary number of notes. // Storage for an arbitrary number of notes. private ArrayList notes; private ArrayList notes; /** /** * Perform any initialization required for the * Perform any initialization required for the * notebook. * notebook. */ */ public Notebook() public Notebook() { notes = new ArrayList (); notes = new ArrayList (); }......} java.util package A class within the java.util package Arraylist Type argument, supplied to Arraylist Arraylist No arguments needed for this Arraylist constructor
9
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Collections We specify: We specify: the type of collection: ArrayList the type of collection: ArrayList the type of objects it will contain: the type of objects it will contain: We say: “ArrayList of String” We say: “ArrayList of String”
10
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Object Structures with Collections
11
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Adding a Third Note myBook.storeNote ("11:30 meet John")
12
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Adding a Third Note Result:
13
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Features of the Collection It increases its capacity as necessary. It increases its capacity as necessary. It keeps a private count … which we can get using the size() accessor. It keeps a private count … which we can get using the size() accessor. It keeps the objects in the order they are added. It keeps the objects in the order they are added. Details of how all this is done are hidden: Details of how all this is done are hidden: Does that matter? Does not knowing how prevent us from using it?Does that matter? Does not knowing how prevent us from using it?
14
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Using the Collection public class Notebook { private ArrayList notes; private ArrayList notes;...... public void storeNote(String note) public void storeNote(String note) { notes.add(note); notes.add(note); } public int numberOfNotes() public int numberOfNotes() { return notes.size(); return notes.size(); }......} Adding a new note (delegation) Returning the number of notes (delegation)
15
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Index Numbering
16
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Retrieve the note Index validity checks Retrieving an Object public void showNote (int noteNumber) { if (noteNumber < 0) { if (noteNumber < 0) { // This is not a valid note number, do nothing. // This is not a valid note number, do nothing. } else if (noteNumber < numberOfNotes()) { else if (noteNumber < numberOfNotes()) { // This is a valid note number, print it. // This is a valid note number, print it. System.out.println (notes.get(noteNumber)); System.out.println (notes.get(noteNumber)); } else { else { // This is not a valid note number, do nothing. // This is not a valid note number, do nothing. }}
17
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Remove the note Index validity checks Removing an Object public void removeNote (int noteNumber) { if (noteNumber < 0) { if (noteNumber < 0) { // This is not a valid note number, do nothing. // This is not a valid note number, do nothing. } else if (noteNumber < numberOfNotes()) { else if (noteNumber < numberOfNotes()) { // This is a valid note number, print it. // This is a valid note number, print it. notes.remove(noteNumber)); notes.remove(noteNumber)); } else { else { // This is not a valid note number, do nothing. // This is not a valid note number, do nothing. }}
18
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Index Numbering myBook.removeNote (1)
19
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Removal affects Numbering myBook.removeNote (1)
20
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling myBook.removeNote (1) Removal affects Numbering
21
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Removal affects Numbering No change
22
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Generic Classes Collection classes are examples of parameterised or generic classes (types). Collection classes are examples of parameterised or generic classes (types). ArrayList implements list functionality: ArrayList implements list functionality: it has methods add, get, size, etc. it has methods add, get, size, etc. The parameter says what kind of data we want the list to hold: The parameter says what kind of data we want the list to hold: ArrayList ArrayList etc. etc.
23
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Collection Classes – Review Collections allow an arbitrary number of objects to be stored. Collections allow an arbitrary number of objects to be stored. Java’s class libraries contain tried-and- tested collection classes. Java’s class libraries contain tried-and- tested collection classes. Java’s class libraries are called packages. Java’s class libraries are called packages. We have used the ArrayList class from the java.util package. We have used the ArrayList class from the java.util package.
24
Objects First with Java - A Practical Introduction using BlueJ, © David J. Barnes, Michael Kölling Items may be added and removed. Items may be added and removed. Each item has an index. Each item has an index. Index values may change if items are removed (or further items added). Index values may change if items are removed (or further items added). The main ArrayList methods are add, get, remove and size. The main ArrayList methods are add, get, remove and size. ArrayList is a parameterized or generic type. ArrayList is a parameterized or generic type. Collection Classes – Review
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.