42 lines
840 B
Plaintext
42 lines
840 B
Plaintext
By contract, any implementation of the ``++java.util.Iterator.next()++`` method should throw a ``++NoSuchElementException++`` exception when the iteration has no more elements. Any other behavior when the iteration is done could lead to unexpected behavior for users of this ``++Iterator++``.
|
|
|
|
|
|
== Noncompliant Code Example
|
|
|
|
----
|
|
public class MyIterator implements Iterator<String>{
|
|
...
|
|
public String next(){
|
|
if(!hasNext()){
|
|
return null;
|
|
}
|
|
...
|
|
}
|
|
}
|
|
----
|
|
|
|
|
|
== Compliant Solution
|
|
|
|
----
|
|
public class MyIterator implements Iterator<String>{
|
|
...
|
|
public String next(){
|
|
if(!hasNext()){
|
|
throw new NoSuchElementException();
|
|
}
|
|
...
|
|
}
|
|
}
|
|
----
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
include::message.adoc[]
|
|
|
|
endif::env-github,rspecator-view[]
|