/
Because linked data structures are particularly appropriate for sequen Because linked data structures are particularly appropriate for sequen

Because linked data structures are particularly appropriate for sequen - PDF document

ellena-manuel
ellena-manuel . @ellena-manuel
Follow
405 views
Uploaded On 2016-06-17

Because linked data structures are particularly appropriate for sequen - PPT Presentation

Returns an iterator over the elements ID: 365800

Returns iterator over the

Share:

Link:

Embed:

Download Presentation from below link

Download Pdf The PPT/PDF document "Because linked data structures are parti..." is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Implementing an Iterator��27 February 2019��OSU CSE Loose EndsIn implementing several kernel interfaces so far, you have been given code in the skeletons for the methodThe code for this method is stylized and sometimeseasy to adapt to a new situation, even if the code itself is hardly transparent!Several new Java issues arise ...��27 February 2019��OSU CSE IteratorsRecall: iterators offer a special way of getting sequential access to all elements/entries of a collectionBecause linked data structures are particularly appropriate for sequential access, the code is a good place to examine how iterators can be implemented��27 February 2019��OSU CSE Contract for Iterator&#x-3.9;&#x 000;T iterator()Returns an iterator over the elements.Ensures:this.seenthis.unseenthis.right&#x-3.9;&#x 000;&#x-3.9;&#x 000;27 February 2019&#x-3.9;&#x 000;&#x-3.9;&#x 000;OSU CSE Contract for Iterator&#x-3.9;&#x 000;T iterator()Returns an iterator over the elements.Ensures:this.seenthis.unseenthis.right&#x-3.9;&#x 000;&#x-3.9;&#x 000;27 February 2019&#x-3.9;&#x 000;&#x-3.9;&#x 000;OSU CSE is an interface in the Java libraries (in the package Contract for Iterator&#x-3.9;&#x 000;T iterator()Returns an iterator over the elements.Ensures:this.seenthis.unseenthis.right&#x-3.9;&#x 000;&#x-3.9;&#x 000;27 February 2019&#x-3.9;&#x 000;&#x-3.9;&#x 000;OSU CSE These two variables stand forthe already seen and the not yet seen while using the iterator. ForEach LoopsSince extends the interface , you may write a foreach loop to “see” all elements of (T x : s) {// do something with x, but do// not call methods on s or// change the value of x��27 February 2019��OSU CSE ForEach LoopsSince extends the interface , you may write a foreach loop to “see” all elements of T x : s// do something with x, but do// not call methods on s or// change the value of x��27 February 2019��OSU CSE This declares as a local variable of type in the loop; on each iteration, is aliasedto a different element of ForEach LoopsSince extends the interface , you may write a foreach loop to “see” all elements of (T x : s) {// do something with x, but not call methods on s orchange the value of x��27 February 2019��OSU CSE The restrictions on what you may do with in the loop body are critical; do not forget about them! How a ForEach Loop WorksThe foreach loop above is actually syntactic sugar for the following code:Iterator&#xT4.5;&#x 000; it = s.iterator();whileit.hasNext()) {T x = it.next();// do something with x, but do// not call methods on s or// change the value of x&#xT4.5;&#x 000;&#xT4.5;&#x 000;27 February 2019&#xT4.5;&#x 000;&#xT4.5;&#x 000;OSU CSE How a ForEach Loop WorksThe foreach loop above is actually syntactic sugar for the following code:Iterator&#xT4.5;&#x 000; it = s.iterator();whileit.hasNext()) {T x = it.next();// do something with x, but do// not call methods on s or// change the value of x&#xT4.5;&#x 000;&#xT4.5;&#x 000;27 February 2019&#xT4.5;&#x 000;&#xT4.5;&#x 000;OSU CSE method for returns a value of type How a ForEach Loop WorksThe foreach loop above is actually syntactic sugar for the following code:Iterator&#xT4.5;&#x 000; it = s.iterator();whileit.hasNextT x = it.next();// do something with x, but do// not call methods on s or// change the value of x&#xT4.5;&#x 000;&#xT4.5;&#x 000;27 February 2019&#xT4.5;&#x 000;&#xT4.5;&#x 000;OSU CSE methods of this variable are used in the iteration. Iterating With This code has the following properties:It introduces aliases, so you must be careful to “follow the rules”; specifically, the loop body should not call any methods on If what you want to do to each element is to change it (when is a mutable type), then the approach does not work because the loop body should not change the value of With , you could just use the kernel methods to visit the entries in the same order��27 February 2019��OSU CSE The InterfaceFor the method, the kernel class returns a reference to an instance of nested class) that implements the interfaceThe code in that class implements these methods:booleanhasNextT next()voidremove()��27 February 2019��OSU CSE The InterfaceFor the method, the kernel class returns a reference to an instance of nested class) that implements the interfaceThe code in that class implements these methods:booleanhasNextT next()voidremove()��27 February 2019��OSU CSE method is described as “optional” in the interface , and we do not support” it because doingso can cause serious problems. hasNextbooleanhasNextReturns iffthe iteration has more elements (i.e., there are any “unseen” elements).Ensures:hasNextthis.unseen/=&#x 000; )&#x 000;&#x 000;27 February 2019&#x 000;&#x 000;OSU CSE next()Returns the next element in the iteration (i.e., the next “unseen” element, which becomes a “seen” element).Aliases: reference returned by Updates: (i.e., the iterator, not the collection) Requires:this.unseen/= &#x -5 ;Ensures:this.seenthis.unseenthis.seenthis.unseenthis.seenthis.seen* e&#xn5 0;xt &#xn5 0;&#xn5 0;27 February 2019&#xn5 0;&#xn5 0;OSU CSE Iterator for ��27 February 2019��OSU CSE��18&#x/BBo;&#xx [5;.32; 4;.5;Α ;ͨ.;䔢&#x 443;&#x.151; ]/;&#xSubt;&#xype ;&#x/Foo;&#xter ;&#x/Typ; /P; gin; tio;&#xn 00;&#x/BBo;&#xx [5;.32; 4;.5;Α ;ͨ.;䔢&#x 443;&#x.151; ]/;&#xSubt;&#xype ;&#x/Foo;&#xter ;&#x/Typ; /P; gin; tio;&#xn 00;this= (.4;&#x 000;8怀, ) ?data this lastLeft finish 1leftLen 1rightLennext 18data 6data nextnext Iterator for ��27 February 2019��OSU CSE��19&#x/BBo;&#xx [5;.32; 4;.5;Α ;ͨ.;䔢&#x 443;&#x.151; ]/;&#xSubt;&#xype ;&#x/Foo;&#xter ;&#x/Typ; /P; gin; tio;&#xn 00;&#x/BBo;&#xx [5;.32; 4;.5;Α ;ͨ.;䔢&#x 443;&#x.151; ]/;&#xSubt;&#xype ;&#x/Foo;&#xter ;&#x/Typ; /P; gin; tio;&#xn 00;this= (.4;&#x 000;8怀, ) ?data this lastLeft finish 1leftLen 1rightLennext 18data 6data nextnext current result of The object created by a call to is an instance of the nested class Iterator for ��27 February 2019��OSU CSE��20&#x/BBo;&#xx [5;.32; 4;.5;Α ;ͨ.;䔢&#x 443;&#x.151; ]/;&#xSubt;&#xype ;&#x/Foo;&#xter ;&#x/Typ; /P; gin; tio;&#xn 00;&#x/BBo;&#xx [5;.32; 4;.5;Α ;ͨ.;䔢&#x 443;&#x.151; ]/;&#xSubt;&#xype ;&#x/Foo;&#xter ;&#x/Typ; /P; gin; tio;&#xn 00;this= (.4;&#x 000;8怀, ) ?data this lastLeft finish 1leftLen 1rightLennext 18data 6data nextnext current result of ... and it holds a reference to the node that has the first entry in Iterator for ��27 February 2019��OSU CSE��21&#x/BBo;&#xx [5;.32; 4;.5;Α ;ͨ.;䔢&#x 443;&#x.151; ]/;&#xSubt;&#xype ;&#x/Foo;&#xter ;&#x/Typ; /P; gin; tio;&#xn 00;&#x/BBo;&#xx [5;.32; 4;.5;Α ;ͨ.;䔢&#x 443;&#x.151; ]/;&#xSubt;&#xype ;&#x/Foo;&#xter ;&#x/Typ; /P; gin; tio;&#xn 00;this= (.4;&#x 000;8怀, ) ?data this lastLeft finish 1leftLen 1rightLennext 18data 6data nextnext current result of method returns the data in that node, also advancing to the next node. Iterator for ��27 February 2019��OSU CSE��22&#x/BBo;&#xx [5;.32; 4;.5;Α ;ͨ.;䔢&#x 443;&#x.151; ]/;&#xSubt;&#xype ;&#x/Foo;&#xter ;&#x/Typ; /P; gin; tio;&#xn 00;&#x/BBo;&#xx [5;.32; 4;.5;Α ;ͨ.;䔢&#x 443;&#x.151; ]/;&#xSubt;&#xype ;&#x/Foo;&#xter ;&#x/Typ; /P; gin; tio;&#xn 00;this= (.4;&#x 000;8怀, ) ?data this lastLeft finish 1leftLen 1rightLennext 6data nextnext current result of 18data method checks whether is (which, in this case, indicates that the last entry has been seen already). A New Java IssueIn the code inside the nested class List2Iterator, there are two references named , so the name is ambiguous!The name denotes the object of type (the nested class)The qualified name denotes the object of type (the enclosing class)See this line of code in the List2Iterator constructor:.current= List2.this.preFront.next;��27 February 2019��OSU CSE A New Java IssueIn the code inside the nested class List2Iterator, there are two references named , so the name is ambiguous!The name denotes the object of type (the nested class)The qualified name denotes the object of type (the enclosing class)See this line of code in the List2Iterator constructor:.current= List2.this.preFront.next;��27 February 2019��OSU CSE The class has an instance variable named , and this is it. A New Java IssueIn the code inside the nested class List2Iterator, there are two references named , so the name is ambiguous!The name denotes the object of type (the nested class)The qualified name denotes the object of type (the enclosing class)See this line of code in the List2Iterator constructor:.currentthis.preFront.next;��27 February 2019��OSU CSE The class has an instance variable named , and this is it. ResourcesJava Libraries API: http://docs.oracle.com/javase/8/docs/api/ ��27 February 2019��OSU CSE