CSE 143 Lecture 16 Iterators; Grammars reading: 11.1, 15.3, 16.5 slides created by Marty Stepp

Slides:



Advertisements
Similar presentations
Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Advertisements

Double-Linked Lists and Circular Lists
Chapter 6 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
Fall 2007CS 2251 Lists and the Collection Interface Chapter 4.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
CSE 143 Lecture 15 Sets and Maps reading: ; 13.2 slides created by Marty Stepp
CSE 143 Lecture 7 Sets and Maps reading: ; 13.2 slides created by Marty Stepp
Chapter 19 Java Data Structures
CSE 143 Lecture 15 Sets and Maps; Iterators reading: ; 13.2; 15.3; 16.5 slides adapted from Marty Stepp
Building Java Programs Chapter 11 Java Collections Framework.
Collections F The limitations of arrays F Java Collection Framework hierarchy  Use the Iterator interface to traverse a collection  Set interface, HashSet,
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
CSE 143 Lecture 11 Maps Grammars slides created by Alyssa Harding
CSE 143 Lecture 4 ArrayList Reading: 10.1 slides created by Marty Stepp
CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.
Building Java Programs Chapter 11 Lecture 11-1: Sets and Maps reading:
CSE 373: Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
1 Building Java Programs Java Collections Framework.
LinkedList Many slides from Horstmann modified by Dr V.
1 Chapter 20 Lists, Stacks, Queues Lecture 7 Dr. Musab Zghoul برمجة هيكلية.
Collections in Java. 2 Collections Hierarchy > ArrayListVector Stack LinkedList > Arrays Collections.
CS-2852 Data Structures LECTURE 7A Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
CSE 143 Lecture 14 (B) Maps and Grammars reading: 11.3 slides created by Marty Stepp
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 13 Implementing.
Building Java Programs Chapter 11 Java Collections Framework Copyright (c) Pearson All rights reserved.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 8 Lists, Iterators, and Doubly Linked Lists.
Collections Mrs. C. Furman April 21, Collection Classes ArrayList and LinkedList implements List HashSet implements Set TreeSet implements SortedSet.
1 TCSS 342, Winter 2005 Lecture Notes Linked List Implementation Weiss Ch. 6, pp Weiss Ch. 17, pp
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
List Interface and Linked List Mrs. Furman March 25, 2010.
CSE 143 Lecture 14 Maps/Sets; Grammars reading: slides adapted from Marty Stepp and Hélène Martin
Iteration Abstraction SWE Software Construction Fall 2009.
CSE 143 Lecture 12: Sets and Maps reading:
CS 46B: Introduction to Data Structures July 21 Class Meeting Department of Computer Science San Jose State University Summer 2015 Instructor: Ron Mak.
CSE 373 Data Structures and Algorithms Lecture 1: Introduction; ADTs; Stacks; Eclipse.
CSE 143 Lecture 11: Sets and Maps reading:
Building Java Programs Chapter 11 Java Collections Framework Copyright (c) Pearson All rights reserved.
Iterators. Iterator  An iterator is any object that allows one to step through each element in a list (or, more generally, some collection).
Chapter 3 – Describing Syntax CSCE 343. Syntax vs. Semantics Syntax: The form or structure of the expressions, statements, and program units. Semantics:
Building Java Programs
CSE 143 Lecture 14 Interfaces; Abstract Data Types (ADTs)
CSE 373 Implementing a Stack/Queue as a Linked List
Wednesday Notecards. Wednesday Notecards Wednesday Notecards.
slides created by Marty Stepp and Hélène Martin
CSE 373: Data Structures and Algorithms
Road Map - Quarter CS Concepts Data Structures Java Language
CSE 143 Lecture 27: Advanced List Implementation
Lecture 26: Advanced List Implementation
slides adapted from Marty Stepp
Maps Grammars Based on slides by Marty Stepp & Alyssa Harding
CSE 143 Lecture 14 More Recursive Programming; Grammars
Iteration Abstraction
Building Java Programs
Welcome to CSE 143! Go to pollev.com/cse143.
Exercise Write a program that counts the number of unique words in a large text file (say, Moby Dick or the King James Bible). Store the words in a collection.
CSE 373 Java Collection Framework, Part 2: Priority Queue, Map
slides created by Marty Stepp
slides created by Alyssa Harding
Plan for Lecture Review code and trace output
slides created by Marty Stepp
slides created by Marty Stepp
Building Java Programs
CSE 143 Lecture 15 Sets and Maps; Iterators reading: ; 13.2
CSE 143 Lecture 8 Iterators; Comparable reading: 11.2; 10.2
slides adapted from Marty Stepp and Hélène Martin
Iterators Dan Fleck.
TCSS 143, Autumn 2004 Lecture Notes
CSE 143 Lecture 21 Advanced List Implementation
Presentation transcript:

CSE 143 Lecture 16 Iterators; Grammars reading: 11.1, 15.3, 16.5 slides created by Marty Stepp

Iterators reading: 11.1; 15.3; 16.5

3 Examining sets and maps elements of Java Set s and Map s can't be accessed by index –must use a "foreach" loop: Set scores = new HashSet (); for (int score : scores) { System.out.println("The score is " + score); } –Problem: foreach is read-only; cannot modify set while looping for (int score : scores) { if (score < 60) { // throws a ConcurrentModificationException scores.remove(score); } }

4 Iterators (11.1) iterator: An object that allows a client to traverse the elements of any collection. –Remembers a position, and lets you: get the element at that position advance to the next position remove the element at that position index value size6 list current element:9 current index:2 iterator set "the" "to" "from" "we" current element:"from" next element:"the" iterator

5 Iterator methods Iterator interface in java.util –every collection has an iterator() method that returns an iterator over its elements Set set = new HashSet ();... Iterator itr = set.iterator();... hasNext() returns true if there are more elements to examine next() returns the next element from the collection (throws a NoSuchElementException if there are none left to examine) remove() removes the last value returned by next() (throws an IllegalStateException if you haven't called next() yet)

6 Iterator example Set scores = new TreeSet (); scores.add(94); scores.add(38); // Jenny scores.add(87); scores.add(43); // Marty scores.add(72);... Iterator itr = scores.iterator(); while (itr.hasNext()) { int score = itr.next(); System.out.println("The score is " + score); // eliminate any failing grades if (score < 60) { itr.remove(); } System.out.println(scores); // [72, 87, 94]

7 Iterator example 2 Map scores = new TreeMap (); scores.put("Jenny", 38); scores.put("Stef", 94); scores.put("Greg", 87); scores.put("Marty", 43); scores.put("Angela", 72);... Iterator itr = scores.keySet().iterator(); while (itr.hasNext()) { String name = itr.next(); int score = scores.get(name); System.out.println(name + " got " + score); // eliminate any failing students if (score < 60) { itr.remove(); // removes name and score } System.out.println(scores); // {Greg=87, Stef=94, Angela=72}

8 A surprising example What's bad about this code? List list = new LinkedList ();... (add lots of elements)... for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); } front= datanext 42 datanext -3 datanext 17 element 0element 1element 2

9 Iterators and linked lists Iterators are particularly useful with linked lists. –The previous code is O(N 2 ) because each call on get must start from the beginning of the list and walk to index i. –Using an iterator, the same code is O(N). The iterator remembers its position and doesn't start over each time. current element:-3 current index: 1 iterator front= datanext 42 datanext -3 datanext 17 element 0element 1element 2

10 Exercise Modify the Book Search program from last lecture to eliminate any words that are plural or all-uppercase from the collection. Modify the TA quarters experience program so that it eliminates any TAs with 3 quarters or fewer of experience.

11 ListIterator ListIterator li = myList.listIterator(); lists have a more powerful ListIterator with more methods –can iterate forwards or backwards –can add/set element values (efficient for linked lists) add( value ) inserts an element just after the iterator's position hasPrevious() true if there are more elements before the iterator nextIndex() the index of the element that would be returned the next time next is called on the iterator previousIndex() the index of the element that would be returned the next time previous is called on the iterator previous() returns the element before the iterator (throws a NoSuchElementException if there are none) set( value ) replaces the element last returned by next or previous with the given value

Languages and Grammars

13 Languages and grammars (formal) language: A set of words or symbols. grammar: A description of a language that describes which sequences of symbols are allowed in that language. –describes language syntax (rules) but not semantics (meaning) –can be used to generate strings from a language, or to determine whether a given string belongs to a given language

14 Backus-Naur (BNF) Backus-Naur Form (BNF): A syntax for describing language grammars in terms of transformation rules, of the form: ::= |... | –terminal: A fundamental symbol of the language. –non-terminal: A high-level symbol describing language syntax, which can be transformed into other non-terminal or terminal symbol(s) based on the rules of the grammar. –developed by two Turing-award-winning computer scientists in 1960 to describe their new ALGOL programming language

15 An example BNF grammar ::= ::=Marty | Victoria | Stuart | Jessica ::=cried | slept | belched Some sentences that could be generated from this grammar: Marty slept Jessica belched Stuart cried

16 BNF grammar version 2 ::= ::= | ::=Marty | Victoria | Stuart | Jessica ::=a | the ::=ball | hamster | carrot | computer ::=cried | slept | belched Some sentences that could be generated from this grammar: the carrot cried Jessica belched a computer slept

17 BNF grammar version 3 ::= ::= | ::=Marty | Victoria | Stuart | Jessica ::=a | the ::=silly | invisible | loud | romantic ::=ball | hamster | carrot | computer ::=cried | slept | belched Some sentences that could be generated from this grammar: the invisible carrot cried Jessica belched a computer slept a romantic ball belched

18 Grammars and recursion ::= ::= | ::=Marty | Victoria | Stuart | Jessica ::=a | the ::= | ::=silly | invisible | loud | romantic ::=ball | hamster | carrot | computer ::=cried | slept | belched Grammar rules can be defined recursively, so that the expansion of a symbol can contain that same symbol. –There must also be expressions that expand the symbol into something non-recursive, so that the recursion eventually ends.

19 Grammar, final version ::= ::= | ::=the|a ::= | ::=big|fat|green|wonderful|faulty|subliminal ::=dog|cat|man|university|father|mother|child ::=John|Jane|Sally|Spot|Fred|Elmo ::= | ::=hit|honored|kissed|helped ::=died|collapsed|laughed|wept Could this grammar generate the following sentences? Fred honored the green wonderful child big Jane wept the fat man fat Generate a random sentence using this grammar.

20 Sentence generation Fred honored the childgreen wonderful