1 / 5

ITERATORS

ITERATORS. Methods. The standard iterator has 3 basic methods. -next(): advances the iterator one element up and returns the object that it passed over. - hasNext ():returns true if there is an element after the iterator’s position, else it will return false .

paytah
Download Presentation

ITERATORS

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ITERATORS

  2. Methods • The standard iterator has 3 basic methods -next(): advances the iterator one element up and returns the object that it passed over -hasNext():returns true if there is an element after the iterator’s position, else it will return false -remove(): removes and returns the last element that was passed over by next()

  3. Implementations • Here is an example of how we can use the iterator to display every element in the list

  4. Your list with elements represented with the letter e Here it is true e1 e2 e3 e4 e5 e6 while(hasNext()) – this checks throughout the loop if there is an element following the iterator Because it is true, we can call the next() method and advance the iterator forward After we call next(), we call the remove() method and take away element that we passed over. We can repeat these steps in a loops to remove all of the elements We now add an iterator to the list by using the list.iterator() method We can use the 3 previously mentioned methods in a loop to systematically return and remove the elements form the list When called the iterator always starts at the position before the first element Once we reach the end, our list is empty and the loop finishes

  5. HUZZAH NOW YOU’RE THINKING WITH ITERATORS

More Related