Download presentation

Presentation is loading. Please wait.

Published byMia Byrd Modified over 2 years ago

1
Chapter 11 Lists and iteration

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

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

4
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)

5

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

7
List specification (cont.) public Student get(int i) The element at the specified position. require:0<=i && i

8
List specification (cont.) public void remove(int i) Remove the element at the specified position. require: 0<=i && i

9
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) …

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

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

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

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

14
while example (cont.)...

15

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

17

18
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()

19
Determining if an object is in a List public boolean contains (StudentList s) { int i; int length; length = this.size(); i=0; while ( i

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

21
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?

22
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

23
Two meanings of equality

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

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

26
Loop structure initialization while (condition) { body } conclusion

27
for statement for ( initialization; condition; updateStatement ) statement

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

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

30
Glossary

31
Glosssary (cont.)

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google