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

## Presentation on theme: "Chapter 11 Lists and iteration. This chapter discusses n Managing collections of objects. n The fundamental container object is called a list. n Fundamental."— Presentation transcript:

Chapter 11 Lists and iteration

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

Lists n A list is a container that holds a finite sequence of values all of the same type. n List properties: u A list is finite, with zero or more elements. u A list is a sequence. u A list is homogeneous. n container: an object whose purpose is to contain other objects. n 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)

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 { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/1/244949/slides/slide_7.jpg", "name": "List specification (cont.) public Student get(int i) The element at the specified position.", "description": "require:0<=i && i

List specification (cont.) public void remove(int i) Remove the element at the specified position. require: 0<=i && i { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/1/244949/slides/slide_8.jpg", "name": "List specification (cont.) public void remove(int i) Remove the element at the specified position.", "description": "require: 0<=i && i

List specification (cont.) n 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 n iteration: a process in which an operation is performed several times. n Syntax: while ( condition ) statement

while statement (cont.) n The component statement is called the body. n Executing the body should have the potential of changing the value of the condition. n 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; } n 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 Students 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.)...

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

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() { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/1/244949/slides/slide_18.jpg", "name": "Finding the minimum /** * The lowest final exam grades of the * specified Students.", "description": "* 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()

Determining if an object is in a List public boolean contains (StudentList s) { int i; int length; length = this.size(); i=0; while ( i { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/1/244949/slides/slide_19.jpg", "name": "Determining if an object is in a List public boolean contains (StudentList s) { int i; int length; length = this.size(); i=0; while ( i

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

What does equal mean? (cont.) n Consider a Date class, that has components day, month, and year. n 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.) n 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; returnthis.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);

Weve covered n How to use a List. u summing elements. u finding an element. u removing elements. n while loops. n Equality. The instanceof operator. n for statements.

Glossary

Glosssary (cont.)

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

Similar presentations