Chapter 11
Lists and iteration

This chapter discusses
Managing collections of objects.
The fundamental container object is called a list.
Fundamental properties of lists.
Specifications for a typical List class.
How to perform an iterative process with a list.
The “while” statement and “for loop.”

Lists
A list is a container that holds a finite sequence of values all of the same type.
List properties:
A list is finite, with zero or more elements.
A list is a sequence.
A list is homogeneous.
container: an object whose purpose is to contain other objects.
list: a container in which the elements are kept in sequence.

List specification
public class StudentList
A finite list of Students.
public StudentList ()
Create an empty StudentList.
public int size ()
Number of elements in List.
ensure:
  this.size() >= 0
public boolean isEmpty ()
List contains no elements.
this.isEmpty()==(this.size()==0)

Slide 5

List specification (cont.)
(a) StudentList sl; //initially //null
(b) sl = new StudentList();
//Empty List

List specification (cont.)
public Student get(int i)
The element at the specified position.
require:0<=i && i<this.size()
public void append (Student s)
Append the specified Student to the end of this List.
require:
  s!=null
ensure:
    this.size()==old.size+1
    this.get(this.size()-1) == s
Anything not mentioned does not change!

List specification (cont.)
public void remove(int i)
Remove the element at the specified position.
require:
  0<=i && i<this.size()
ensure:
  this.size()==old.size-1
  for i <= j < this.size()
      this.get(j) == old.get(j+1)
public void set (int i, Student s)
Replace the element at the specified position with the specified Student.
require:
  0 <= i && i < this.size()
  s != null
ensure:
  this.get(i) == s

List specification (cont.)
Our specifications are fairly ubiquitous.  Just substitute the class name Student with another class.  Change any reference to an Student object with a reference to  an object of another class.
public NewObject get (int i)
public void append (NewObject n)
public void set (int i, NewObject n)

while  statement
iteration: a process in which an operation is performed several times.
Syntax:
while ( condition )
statement

while  statement (cont.)
The component statement is called the body.
Executing the body should have the potential of changing the value of the condition.
It is possible that the condition of a “while” statement will remain true no matter now many time the body is executed.  This is an “infinite loop.”

while loops and lists.
while( more list elements to process )
process the next element
e.g.
int index;
index = 0;
while (index < list.size()) {
process list.get(index);
index = index + 1;
}
We must guarantee that the while condition eventually will become false.

while example - summing items of a List
public double finalAverage(StudentList students)
The average (mean) of the final exam grades of the specified Students.
require:
  students.size() > 0
public int finalExam ()
This Student’s grade on the final exam.
public double finalAverage(StudentList students)
{
int i, sum, count;
count = students.size();
sum = 0;
i = 0;
while ( i < count) {
sum = sum + students.get(i).finalExam();
i = i+1;
}
return (double)sum / (double)count;
}

while example (cont.)

Slide 15

Summing selected elements of a List
Consider what to do if some students did not take the final exam.

Slide 17

Finding the minimum
/**
 * The lowest final exam grades of the
 * specified Students.
 * require:
 *   students.size() > 0
 */
public int  minFinalExam (StudentList students){
  int i;
  int low;
  low = students.get(0).finalExam();
  i=1;
  while ( i < students.size()){
    if (students.get(i).finalExam()<low)
      low=students.get(i).finalExam();
    i = i+1;
  }
}

Determining if an object is in a List
public boolean  contains (StudentList s)
{
  int i;
  int length;
  length = this.size();
  i=0;
  while ( i<length && get(i)!=s )
  i = i+1;
  return i < length;
}

What does “equal” mean?
Two reference values are equal if they refer to the same object.

What does “equal” mean? (cont.)
Consider a Date class, that has components day, month, and year.
If distinct Date objects are created to represent the same date, would these objects be “equal” ? Must they refer to the same object?

What does “equal” mean? (cont.)
To determine if they are “equal” dates, we must compare their day, month, and year.
public boolean equals (Object obj){
  Require.condition(obj
instanceof Date);
  Date d = (Date)obj;
return this.year()==d.year() &&
    this.month()==d.month() &&
this.day()==d.day();
}
// instanceof returns true if the object is
// an instance of
// the class

Two meanings of equality

indexOf method
public int indexOf (Student obj) {
int i;
int length;
length = this.size();
i = 0;
while (i < length && !obj.equals(get(i)))
i = i+1;
if ( i < length)
return i;
else
return -1;// item not found
}

Removing duplicates
public void removeDuplicates(StudentList l)
{
int i; //index
Student item; //Check for duplicates
int j; //index
//invariant: i < j
i = 0;
while (i < list.size()) {
  item = list.get(i);
  j = i + 1;
while (j < list.size())
  if (item.equals(list.get(j)))
list.remove(j);
  else
j = j+1;
i = i+1;
}
}

Loop structure
initialization
while (condition) {
body
}
conclusion

for statement
for ( initialization; condition; updateStatement )
statement

for statement example
int i;
for ( i = 0; i < list.size(); i = i+1)
process list.get(i);

We’ve covered
How to use a List.
summing elements.
finding an element.
removing elements.
while loops.
Equality.
The instanceof  operator.
for statements.

Glossary

Glosssary (cont.)