DOC PREVIEW
UT Arlington CSE 3302 - Control lecture notes

This preview shows page 1-2-3-4-5-33-34-35-36-67-68-69-70-71 out of 71 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 71 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 71 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 71 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 71 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 71 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 71 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 71 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 71 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 71 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 71 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 71 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 71 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 71 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 71 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 71 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CSE 3302Lecture'17:'Control28'Oct'2010Nate'NystromUniversity'of'Texas'at'ArlingtonFor loopsPascal:for$x$:=$1$to$10$do$$$$writeln(x)C:for$(x$=$1;$x$<$10;$x++)$$$$printf(“%d\n”,$x);2IterationList<String>$counting$=$$$$Arrays.asList(“yan”,$“tan”,$“tethera”,$“methera”,$“pip”);for$(int$i$=$0;$i$<$counting.size();$i++)${$$$$String$s$=$counting.get(i);$$$$println(s);}3Java iteratorsList<String>$counting$=$$$$Arrays.asList(“yan”,$“tan”,$“tethera”,$“methera”,$“pip”);for$(String$s$:$counting)${$$$$println(s);}4Java iteratorsList<String>$counting$=$$$$Arrays.asList(“yan”,$“tan”,$“tethera”,$“methera”,$“pip”);for$(String$s$:$counting)${$$$$println(s);}shorthand'for:for$(Iterator<String>$i$=$counting.iterator();$i.hasNext();$)${$$$$String$s$=$i.next();$$$$println(s);}5Java iteratorsUsing'iterators'is'easy.How'about'wriGng'one?6Writing a Java iteratorImplement'java.util.Iterator:interface$Iterator<T>${$$$$boolean$hasNext();$$$$T$next();$$$$void$remove();$$//$we’ll$ignore$this$one!}7Writing a Java iteratorclass$ArrayListIterator<T>$implements$Iterator<T>${$$$$ArrayList<T>$list;$$$$int$pos$=$0;$$$$boolean$hasNext()${$return$pos$<$list.size();$}$$$$T$next()${$$$$$$$$if$(pos$<$list.size())${$$$$$$$$$$$$pos++;$$$$$$$$$$$$return$list.get(posU1);$$$$$$$$}$$$$$$$$else$throw$new$NoSuchElementException();$$$$}}8Writing a Java iteratorclass%TreeIterator<T>%implements%Iterator<T>%{%%%%Stack<TreeNode<T>>%stack;%%%%TreeIterator(TreeSet<T>%tree)%{%%%%%%%%stack%=%new%Stack<TreeNode<T>>();%%%%%%%%pushLeft(tree.root);%%%%}%%%%void%pushLeft(TreeNode<T>%n)%{%%%%%%%%if%(n%!=%null)%stack.push(n);%%%%%%%%pushLeft(n.left);%%%%}%%%%boolean%hasNext()%{%return%!%stack.isEmpty();%}%%%%T%next()%{%%%%%%%%if%(stack.isEmpty())%throw%new%NoSuchElementException();%%%%%%%%TreeNode<T>%x%=%stack.pop();%%%%%%%%pushLeft(x.right);%%%%%%%%return%x.value;%%%%}}9class%TreeSet<T>%{%%%%%TreeNode<T>%root;%%%%%class%TreeNode<T>%{%%%%%%%%%TreeNode<T>%left,%right;%%%%%%%%%T%value;%%%%%}}Writing a Java iteratorIterator'needs'to'maintain'state◾what'have'I'already'returned?◾what'do'I'return'when'next'invoked?10Hashtable'iteratorWhat'bucket'am'I'currently'in?Where'in'the'bucket'am'I?11A better wayJust'write'a'traversal'of'the'data'structure.At'each'element'and'yield'back'to'the'iterator.12Coroutine iteratorsclass$ArrayList<T>${$$$$T[]$array;$$$$elements()$iterates$T${$$$$$$$$for$(int$i$=$0;$i$<$size;$i++)${$$$$$$$$$$$$yield$array[i];$$$$$$$$}$$$$}}for$(T$x$:$list.elements())${$$$$println(x);}13Syntax'is'Java'with'a'hypoth e1cal'extension'that'adds'a'yield'statement.class$ArrayList<T>${$$$$T[]$array;$$$$elements()$iterates$T${$$$$$$$$for$(int$i$=$0;$i$<$size;$i++)${$$$$$$$$$$$$yield$array[i];$$$$$$$$}$$$$}}for$(T$x$:$list.elements())${$$$$println(x);}yield'suspends'the'elements(),'returns'control'back'to'the'caller.When'iterator'nex


View Full Document

UT Arlington CSE 3302 - Control lecture notes

Documents in this Course
Smalltalk

Smalltalk

11 pages

Syntax

Syntax

5 pages

Syntax

Syntax

5 pages

JAVA

JAVA

57 pages

Semantics

Semantics

41 pages

Control

Control

74 pages

Load more
Download Control lecture notes
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Control lecture notes and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Control lecture notes 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?