object-oriented programming with java
ahmed al-ajeli
grouping objects
introduction to collections
ahmed al-ajeli
lecture 7
2
main concepts to be covered
•collections - especially arraylist.
•builds on the themes from the last chapter:
–abstraction.
–objects creating objects.
–external method calls.
object-oriented programming with java
ahmed al-ajeli
3
the requirement to group objects
•many applications involve collections of objects:
–personal organizers.
–library catalogs.
–student-record systems.
•the number of items to be stored varies.
–items added.
–items deletingd.
4
an organizer for music files
•single-track files may be added.
•there is no pre-defined limit to the number of files/tracks.
•it will tell how many file names are stored in the collection.
•it will list individual file names.
object-oriented programming with java
ahmed al-ajeli
5
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 multiple classes for doing this.
6
collections
•a collection object can store an arbitrary number of other objects.
•we specify:
–the type of collection: arraylist
–the type of objects it will contain:
–private arraylist files
•we say, “arraylist of string”.
object-oriented programming with java
ahmed al-ajeli
7
collections – source code
import java.util.arraylist
public class musicorganizer
{
// storage for an arbitrary number of file names.
private arraylist files
public musicorganizer()
{
files = new arraylist<>()
}
...
}
8
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
–arraylist
–etc.
object-oriented programming with java
ahmed al-ajeli
9
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.
–it’s a convenience for the programmer.
10
object structures with collections
object-oriented programming with java
ahmed al-ajeli
11
adding a third file
12
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?
object-oriented programming with java
ahmed al-ajeli
13
generic classes
•we can use arraylist with any class type: arraylist arraylist arraylist arraylist
•each will store multiple objects of the specific type.
14
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)
object-oriented programming with java
ahmed al-ajeli
15
index numbering
16
retrieving from the collection
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?)
object-oriented programming with java
ahmed al-ajeli
17
removal may affect numbering
state after:
files.remove(1)
18
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, …
object-oriented programming with java
ahmed al-ajeli
19
the musicorganizer- source code
import java.util.arraylist
public class musicorganizer
{
private arraylist files
public musicorganizer()
{
files = new arraylist()
}
public void addfile(string filename)
{
files.add(filename)
}
public int getnumberoffiles()
{
return files.size()
}
20
public void listfile(int index)
{
if(index >= 0 && index < files.size()) {
string filename = files.get(index)
system.out.println(filename)
}
else {
system.out.println(“invalid index!”)
}
}
public void removefile(int index)
{
if(index >= 0 && index < files.size()) {
files.remove(index)
}
else {
system.out.println(“invalid index!”)
}
}
}
object-oriented programming with java
ahmed al-ajeli
21
the musicorganizer- testing
public class test
{
public static void main(string[] args)
{
musicorganizer mymusic = new musicorganizer ()
system.out.println (mymusic.getnumberoffiles())
mymusic.addfile (“morningblues.mp3”)
mymusic.addfile (“dontgo.mp3”)
mymusic.addfile (“matchboxblues.mp3”)
system.out.println (mymusic.getnumberoffiles())
mymusic.listfile (0)
mymusic.removefile (1)
}
}