package ru.job4j.evennumbersiterator;

import java.util.Iterator;
import java.util.NoSuchElementException;

/**.
 * Class EvenNumbersIterator implements Iterator for even numbers.
 * @author Mikhail Vlasov
 * @since 03.06.2018
 * @version 2
 */
public class EvenNumbersIterator implements Iterator {
    /**.
     * Our array.
     */
    private final int[] values;
    /**.
     * An index in the array.
     */
    private int x = 0;

    /**.
     * Constructor.
     * @param values array.
     */
    public EvenNumbersIterator(int[] values) {
        this.values = values;
    }

    /**.
     * Overriden hasNext checks if the next even number exists.
     * @return boolean true if the even number exists.
     */
    @Override
    public boolean hasNext() {
        boolean isEvenNumAvailable = false;

        for (int i = x; i < values.length; i++) {
            if (values[i] % 2 == 0) {
                isEvenNumAvailable = true;
                break;
            }
        }
        return isEvenNumAvailable;
    }

    /**.
     * Overriden next returns next even number if available. Otherwise throws NoSuchElementException.
     * @return Object next element.
     */
    @Override
    public Object next() {
        boolean evenNumFound = false;
        int resultNum = -1;

        if (!hasNext()) {
            throw new NoSuchElementException("Out of array bound.");
        }
        do {
            if (values[x] % 2 == 0) {
                resultNum = values[x++];
                evenNumFound = true;
                break;
            }
            x++;
        } while (hasNext());

        return resultNum;
    }
}

@@ -232,17 +232,17 @@
 version 
-2
+3
 %0A */%0Apub
@@ -773,126 +773,95 @@
     
-boolean isEvenNumAvailable = false;%0A%0A        for (int i = x; i %3C values.length; i++) %7B%0A            if (values%5Bi
+int tempIndex = x;%0A%0A        while (tempIndex %3C values.length && values%5BtempIndex
 %5D %25 2 
-=
+!
 = 0)
@@ -879,119 +879,71 @@
     
-    isEvenNumAvailable = true;%0A                break;%0A            %7D%0A        %7D%0A        return isEvenNumAvailable
+tempIndex++;%0A        %7D%0A        return tempIndex %3C values.length
 ;%0A  
@@ -1154,81 +1154,15 @@
     
- boolean evenNumFound = false;%0A        int resultNum = -1;%0A%0A        if
+while
  (
-!
 hasN
@@ -1174,101 +1174,8 @@
 ) %7B%0A
-            throw new NoSuchElementException(%22Out of array bound.%22);%0A        %7D%0A        do %7B%0A 
     
@@ -1226,20 +1226,14 @@
     
- 
 re
-sultNum =
+turn
  val
@@ -1257,142 +1257,97 @@
     
-     evenNumFound = true;%0A                break;%0A            %7D%0A            x++;%0A        %7D while (hasNext());%0A%0A        return resultNum
+%7D%0A           x++;%0A       %7D%0A       throw new NoSuchElementException(%22Out of array bound.%22)
 ;%0A  

Task 150