diff --git a/Nested/src/Collection.java b/Nested/src/Collection.java new file mode 100644 index 0000000..20026e8 --- /dev/null +++ b/Nested/src/Collection.java @@ -0,0 +1,92 @@ +public class Collection { + public static Integer[] array; + + public Collection(Integer[] args) { + this.array = args; + } + class FirstTaskIterator implements Iterator { + + private int count = 0; + + @Override + public boolean hesNext() { + return count < array.length; + } + + @Override + public Integer next() { + if ((count % 2) == 0) { + return array[count++]; + } + count++; + return 0; + } + } + + class SecondTaskIterator implements Iterator { + + private int count = array.length - 1; + + @Override + public boolean hesNext() { + return count > 0; + } + + @Override + public Integer next() { + int a; + a = array[count]; + count -= 2; + return a; + } + + } + + class ThirdTaskIterator implements Iterator { + + private int count = 0; + + + @Override + public boolean hesNext() { + return count < array.length; + } + + @Override + public Integer next() { + int a = 0; + while (a == 0 && count < array.length) { + if ((count + 1) % 5 == 0 && array[count] % 2 == 0) { + a = array[count] - 100; + } + count++; + } + return a; + } + } + + static class FourthTaskIterator implements Iterator { + + private int count; + + @Override + public boolean hesNext() { + return count < array.length; + } + + @Override + public Integer next() { + int a = 0; + while (a == 0 && a < array.length) { + if (array[count] % 2 == 0) { + a = array[count] - 1; + } + count ++; + } + return a; + } + + } + +} + diff --git a/Nested/src/Iterator.java b/Nested/src/Iterator.java new file mode 100644 index 0000000..e416ca3 --- /dev/null +++ b/Nested/src/Iterator.java @@ -0,0 +1,5 @@ +public interface Iterator { + Integer next(); + + boolean hesNext(); +} diff --git a/Nested/src/Main.java b/Nested/src/Main.java new file mode 100644 index 0000000..872bf63 --- /dev/null +++ b/Nested/src/Main.java @@ -0,0 +1,48 @@ +public class Main { + public static void main(String[] args) { + Collection someCollection = new Collection(new Integer[]{11, 22, 33, 44, 55, 66, 77, 88, 99, 10, 20, 30, 40, 50, 60, 70, 80, 90}); + Iterator firstTaskIterator = someCollection.new FirstTaskIterator(); + Iterator secondTaskIterator = someCollection.new SecondTaskIterator(); + Iterator anonIterator = new Iterator() { + private int count = someCollection.array.length - 1; + + + @Override + public boolean hesNext() { + return count > 0; + } + + @Override + public Integer next() { + int a = 0; + while (a == 0 && count > 0) { + if (someCollection.array[count] % 2 != 0) { + a = someCollection.array[count]; + } + count -= 3; + } + count -= 3; + return a; + } + + + }; + Iterator thirdTaskIterator = someCollection.new ThirdTaskIterator(); + Iterator fourthTaskIterator = new Collection.FourthTaskIterator(); + + + display(firstTaskIterator); + display(secondTaskIterator); + display(anonIterator); + display(thirdTaskIterator); + display(fourthTaskIterator); + + } + static void display(Iterator iterator) { + while (iterator.hesNext()) { + System.out.print(iterator.next() + " "); + } + System.out.println("\n" + "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"); + } +} +