Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Iterator
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Java=== Introduced in the [[Java (programming language)|Java]] JDK 1.2 release, the {{Javadoc:SE|package=java.util|java/util|Iterator}} interface allows the iteration of container classes. Each <code>Iterator</code> provides a {{Javadoc:SE|name=next()|java/util|Iterator|next()}} and {{Javadoc:SE|name=hasNext()|java/util|Iterator|hasNext()}} method,<ref name=Bloch>{{cite book | title= "Effective Java: Programming Language Guide" |last=Bloch| first=Joshua| publisher=Addison-Wesley | edition=third | isbn=978-0134685991| year=2018}}</ref>{{rp|294β295}} and may optionally support a {{Javadoc:SE|name=remove()|java/util|Iterator|remove()}}<ref name=Bloch/>{{rp|262, 266}} method. Iterators are created by the corresponding container class, typically by a method named <code>iterator()</code>.<ref>{{cite web | url = http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Iterator.html | title = java.util: Interface Iterator<E>: Method Summary | publisher = Oracle | access-date = 2012-08-08 }}</ref><ref name=Bloch/>{{rp|99}}<ref name=Bloch/>{{rp|217}} The <code>next()</code> method advances the iterator and returns the value pointed to by the iterator. The first element is obtained upon the first call to <code>next()</code>.<ref name=Bloch/>{{rp|294β295}} To determine when all the elements in the container have been visited the <code>hasNext()</code> test method is used.<ref name=Bloch/>{{rp|262}} The following example shows a simple use of iterators: <syntaxhighlight lang="java"> Iterator iter = list.iterator(); // Iterator<MyType> iter = list.iterator(); // in J2SE 5.0 while (iter.hasNext()) { System.out.print(iter.next()); if (iter.hasNext()) System.out.print(", "); } </syntaxhighlight> To show that <code>hasNext()</code> can be called repeatedly, we use it to insert commas between the elements but not after the last element. This approach does not properly separate the advance operation from the actual data access. If the data element must be used more than once for each advance, it needs to be stored in a temporary variable. When an advance is needed without data access (i.e. to skip a given data element), the access is nonetheless performed, though the returned value is ignored in this case. For collection types that support it, the <code>remove()</code> method of the iterator removes the most recently visited element from the container while keeping the iterator usable. Adding or removing elements by calling the methods of the container (also from the same [[Thread (computer science)|thread]]) makes the iterator unusable. An attempt to get the next element throws the exception. An exception is also thrown if there are no more elements remaining (<code>hasNext()</code> has previously returned false). Additionally, for {{Javadoc:SE|package=java.util|java/util|List}} there is a {{Javadoc:SE|package=java.util|java/util|ListIterator}} with a similar API but that allows forward and backward iteration, provides its current index in the list and allows setting of the list element at its position. The [[Java Platform, Standard Edition|J2SE]] 5.0 release of Java introduced the {{Javadoc:SE|java/lang|Iterable}} interface to support an enhanced <code>for</code> ([[foreach]]) loop for iterating over collections and arrays. <code>Iterable</code> defines the {{Javadoc:SE|name=iterator()|java/lang|Iterable|iterator()}} method that returns an <code>Iterator</code>.<ref name=Bloch/>{{rp|266}} Using the enhanced <code>for</code> loop, the preceding example can be rewritten as <syntaxhighlight lang="java"> for (MyType obj : list) { System.out.print(obj); } </syntaxhighlight> Some containers also use the older (since 1.0) <code>Enumeration</code> class. It provides <code>hasMoreElements()</code> and <code>nextElement()</code> methods but has no methods to modify the container.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)