- collections
- iterators
- loops
- arrays

Alberto Ferrari
Ingegneria dell'Informazione, UniPR
ArrayList<Person>
ArrayList<TicketMachine>
ArrayList<String>
import java.util.ArrayList;
public class MusicOrganizer {
// An ArrayList for storing the file names of music files.
private ArrayList<String> files;
public MusicOrganizer() {
files = new ArrayList<String>();
}
...

public int getNumberOfFiles() {
return files.size();
}
for(ElementType element : collection) {
loop body
}
/**
* Show a list of all the files in the collection.
*/
public void listAllFiles() {
for(String filename : files) {
System.out.println(filename);
}
}
while(boolean condition) {
loop body
}
int index = 0;
boolean found = false;
while(index < myCollection.size() && !found) {
myElement = myCollection.get(index);
if (myElement ...) {
found = true;
...
}
index++;
}
// An ArrayList for storing music tracks.
private ArrayList<Track> tracks;
/** Show a list of all the tracks in the collection. */
public void listAllTracks() {
for(Track track : tracks) {
System.out.println(track.getDetails());
}
}
Iterator<ElementType> it = myCollection.iterator();
while(it.hasNext()) {
// call it.next() to get the next element
// do something with that element
}
import java.util.ArrayList;
import java.util.Iterator;
...
/**
* List all the tracks.
*/
public void listAllTracks() {
Iterator<Track> it = tracks.iterator();
while(it.hasNext()) {
Track t = it.next();
System.out.println(t.getDetails());
}
}

Iterator<Track> it = tracks.iterator();
while(it.hasNext()) {
Track t = it.next();
String artist = t.getArtist();
if(artist.equals(artistToRemove)) {
it.remove();
}
}