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

Slides:



Advertisements
Similar presentations
Homework Answers 1. {3} 2. {1, 3} 5. {3, 4, 6} 6. {} 10. {2, 3, 4}
Advertisements

Chapter 20 Computational complexity. This chapter discusses n Algorithmic efficiency n A commonly used measure: computational complexity n The effects.
Chapter 22 Implementing lists: linked implementations.
Chapter 8 Testing a class. This chapter discusses n Testing in general. n Testing a single class. n Test plans. n Building a test system.
Chapter 23 Organizing list implementations. This chapter discusses n The notion of an iterator. n The standard Java library interface Collection, and.
Chapter 21 Implementing lists: array implementation.
Why not just use Arrays? Java ArrayLists.
Chapter 4: Control Structures I (Selection)
Chapter 7. Binary Search Trees
Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
Linear Lists – Array Representation
Linked Lists Linear collections.
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
For(int i = 1; i
Karel J Robot Chapter 6.
Introduction to Arrays Chapter What is an array? An array is an ordered collection that stores many elements of the same type within one variable.
Two-Dimensional Arrays Chapter What is a two-dimensional array? A two-dimensional array has “rows” and “columns,” and can be thought of as a series.
Chapter 14 Introduction to Collections
1 Specifying Object Interfaces. 2 Major tasks in this stage: --are there any missing attributes or operations? --how can we reduce coupling, make interface.
Chapter 5 Section 1 Sets Basics Set –Definition: Collection of objects –Specified by listing the elements of the set inside a pair of braces. –Denoted.
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
Flow of Control Loops – Chapter 3.2. Java Loop Statements: Outline the while Statement the do-while Statement the for Statement.
1 Stack Data : a collection of homogeneous elements arranged in a sequence. Only the first element may be accessed Main Operations: Push : insert an element.
Chapter 2 Control. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Repetition, quick overview.
04/29/ Introduction to Vectors?... A vector is a dynamic array. - It can be expanded and shrunk as required - A Component of a vector can be accessed.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
The Java Collections Framework (JCF) Introduction and review 1.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
© 2004 Goodrich, Tamassia Stacks. © 2004 Goodrich, Tamassia Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data.
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
Chapter 5: Sequences, Mathematical Induction, and Recursion 5.9 General Recursive Definitions and Structural Induction 1 Erickson.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
Lecture 7 February 24, Javadoc version and author Tags These go in the comments before named classes. –Put your SS# on a separate line from the.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
Ansys Workbench 11 Case Study with Design Optimization ME 520 Fundamentals of Finite Element Analysis.
Click to edit Master text styles Stacks Data Structure.
Thinking Mathematically Basic Set Concepts. A “set” is a collection of objects. Each object is called an “element” of the set. Often the objects in a.
1 CS162: Introduction to Computer Science II Abstract Data Types.
UNIT-V ABSTRACT DATA TYPE 1.LIST 2.STACK 3.QUEUE EC6301-II-ECE-C.
CS2005 Week 7 Lectures Set Abstract Data Type.
Chapter 4 Repetition Statements (loops)
Lecture 1 (UNIT -4) TREE SUNIL KUMAR CIT-UPES.
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Stacks.
Chapter 5 Structures.
Week 2: 10/1-10/5 Monday Tuesday Wednesday Thursday Friday
Specifying Object Interfaces
TCSS 143, Autumn 2004 Lecture Notes
Lists in Python.
Chapter 8 JavaScript: Control Statements, Part 2
CIT Nov-18.
Set-Builder Notation.
Iterator.
Chapter 12 : Lists.
Programming II (CS300) Chapter 07: Linked Lists and Iterators
Object Oriented Programming in java
LabVIEW.
Combinational Circuits
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
Chapter8: Statement-Level Control Structures April 9, 2019
Arrays in Java.
Intro to OOP with Java, C. Thomas Wu By : Zanariah Idrus
Chapter 5 Stack (part 1).
Lists - I The List ADT.
Lists - I The List ADT.
Set – collection of objects
Chapter 3 Vocabulary 3.)Roster form 4.) Set-builder notation 5.) Empty set 6.) Universal set 7.) Complement of a set.
Java Generics & Iterators
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<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 n 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.) 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()<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? 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.)