31
Chapter 11 Lists and iteration

Chapter 11 Lists and iteration. This chapter discusses n Managing collections of objects. n The fundamental container object is called a list. n Fundamental

Embed Size (px)

Citation preview

Page 1: Chapter 11 Lists and iteration. This chapter discusses n Managing collections of objects. n The fundamental container object is called a list. n Fundamental

Chapter 11Lists and iteration

Page 2: Chapter 11 Lists and iteration. This chapter discusses n Managing collections of objects. n The fundamental container object is called a list. n Fundamental

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

Page 3: Chapter 11 Lists and iteration. This chapter discusses n Managing collections of objects. n The fundamental container object is called a list. n Fundamental

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.

Page 4: Chapter 11 Lists and iteration. This chapter discusses n Managing collections of objects. n The fundamental container object is called a list. n Fundamental

List specificationpublic 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)

Page 5: Chapter 11 Lists and iteration. This chapter discusses n Managing collections of objects. n The fundamental container object is called a list. n Fundamental
Page 6: Chapter 11 Lists and iteration. This chapter discusses n Managing collections of objects. n The fundamental container object is called a list. n Fundamental

List specification (cont.)(a) StudentList sl; //initially

//null

(b) sl = new StudentList();

//Empty List

Page 7: Chapter 11 Lists and iteration. This chapter discusses n Managing collections of objects. n The fundamental container object is called a list. n Fundamental

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!

Page 8: Chapter 11 Lists and iteration. This chapter discusses n Managing collections of objects. n The fundamental container object is called a list. n Fundamental

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

Page 9: Chapter 11 Lists and iteration. This chapter discusses n Managing collections of objects. n The fundamental container object is called a list. n Fundamental

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)

Page 10: Chapter 11 Lists and iteration. This chapter discusses n Managing collections of objects. n The fundamental container object is called a list. n Fundamental

while statement iteration: a process in which an

operation is performed several times.

Syntax:while ( condition )

statement

Page 11: Chapter 11 Lists and iteration. This chapter discusses n Managing collections of objects. n The fundamental container object is called a list. n Fundamental

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

Page 12: Chapter 11 Lists and iteration. This chapter discusses n Managing collections of objects. n The fundamental container object is called a list. n Fundamental

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.

Page 13: Chapter 11 Lists and iteration. This chapter discusses n Managing collections of objects. n The fundamental container object is called a list. n Fundamental

while example - summing items of a Listpublic 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;

}

Page 14: Chapter 11 Lists and iteration. This chapter discusses n Managing collections of objects. n The fundamental container object is called a list. n Fundamental

while example (cont.)

...

Page 15: Chapter 11 Lists and iteration. This chapter discusses n Managing collections of objects. n The fundamental container object is called a list. n Fundamental
Page 16: Chapter 11 Lists and iteration. This chapter discusses n Managing collections of objects. n The fundamental container object is called a list. n Fundamental

Summing selected elements of a List Consider what to do if some

students did not take the final exam.

Page 17: Chapter 11 Lists and iteration. This chapter discusses n Managing collections of objects. n The fundamental container object is called a list. n Fundamental
Page 18: Chapter 11 Lists and iteration. This chapter discusses n Managing collections of objects. n The fundamental container object is called a list. n Fundamental

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

Page 19: Chapter 11 Lists and iteration. This chapter discusses n Managing collections of objects. n The fundamental container object is called a list. n Fundamental

Determining if an object is in a Listpublic 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;}

Page 20: Chapter 11 Lists and iteration. This chapter discusses n Managing collections of objects. n The fundamental container object is called a list. n Fundamental

What does “equal” mean? Two reference values are equal if

they refer to the same object.

Page 21: Chapter 11 Lists and iteration. This chapter discusses n Managing collections of objects. n The fundamental container object is called a list. n Fundamental

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?

Page 22: Chapter 11 Lists and iteration. This chapter discusses n Managing collections of objects. n The fundamental container object is called a list. n Fundamental

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

Page 23: Chapter 11 Lists and iteration. This chapter discusses n Managing collections of objects. n The fundamental container object is called a list. n Fundamental

Two meanings of equality

Page 24: Chapter 11 Lists and iteration. This chapter discusses n Managing collections of objects. n The fundamental container object is called a list. n Fundamental

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

}

Page 25: Chapter 11 Lists and iteration. This chapter discusses n Managing collections of objects. n The fundamental container object is called a list. n Fundamental

Removing duplicatespublic void removeDuplicates(StudentList l) {

int i; //indexStudent item; //Check for duplicatesint j; //index

//invariant: i < ji = 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;

}}

Page 26: Chapter 11 Lists and iteration. This chapter discusses n Managing collections of objects. n The fundamental container object is called a list. n Fundamental

Loop structure

initializationwhile (condition) {body

}conclusion

Page 27: Chapter 11 Lists and iteration. This chapter discusses n Managing collections of objects. n The fundamental container object is called a list. n Fundamental

for statement

for ( initialization; condition; updateStatement )

statement

Page 28: Chapter 11 Lists and iteration. This chapter discusses n Managing collections of objects. n The fundamental container object is called a list. n Fundamental

for statement example

int i;

for ( i = 0; i < list.size(); i = i+1)

process list.get(i);

Page 29: Chapter 11 Lists and iteration. This chapter discusses n Managing collections of objects. n The fundamental container object is called a list. n Fundamental

We’ve covered

How to use a List. summing elements. finding an element. removing elements.

while loops. Equality. The instanceof operator. for statements.

Page 30: Chapter 11 Lists and iteration. This chapter discusses n Managing collections of objects. n The fundamental container object is called a list. n Fundamental

Glossary

Page 31: Chapter 11 Lists and iteration. This chapter discusses n Managing collections of objects. n The fundamental container object is called a list. n Fundamental

Glosssary (cont.)