Why write an iterator like that?

I read the Java tutorial on the inner class

http://java.sun.com/docs/books/tutorial/java/javaOO/innerclasses.html

This explains that in the example, "InnerEvenIterator inner class that looks like a standard Java iterator." So, I suppose iterators are pretty common in Java?

I came from the C programming background. I don't understand why a simple loop like this

for(i=0;i <SIZE;i+2){ System.System.out.println(arrayOfInts[i])); } 

was added to iterators (inner class) in two ways. What is the point here?

 public class DataStructure { //create an array private final static int SIZE = 15; private int[] arrayOfInts = new int[SIZE]; public DataStructure() { //fill the array with ascending integer values for (int i = 0; i < SIZE; i++) { arrayOfInts[i] = i; } } public void printEven() { //print out values of even indices of the array InnerEvenIterator iterator = this.new InnerEvenIterator(); while (iterator.hasNext()) { System.out.println(iterator.getNext() + " "); } } //inner class implements the Iterator pattern private class InnerEvenIterator { //start stepping through the array from the beginning private int next = 0; public boolean hasNext() { //check if a current element is the last in the array return (next <= SIZE - 1); } public int getNext() { //record a value of an even index of the array int retValue = arrayOfInts[next]; //get the next even element next += 2; return retValue; } } public static void main(String s[]) { //fill the array with integer values and print out only values of even indices DataStructure ds = new DataStructure(); ds.printEven(); } } 
+1
source share
5 answers

For simple loops over arrays, you will not (usually) use Iterator in Java.

 for(int i=0;i < arrayOfInts.length ; i+2){ System.out.println(arrayOfInts[i])); } 

The idea of ​​an iterator is to separate how the data is stored (maybe not an array) from its consumer (code that wants to iterate over it).

You have the right to say that Iterator is a fairly basic concept in the Java class library, so widespread that from Java5 there is a function for each cycle that supports it. In this loop, the user no longer sees Iterator.

 for(Something element: listOfSomething){ System.out.println(element); } 

If I were to implement a “smooth stepping iterator”, I would base it on a regular iterator so that it can be used with any iterable.

 public class EvenSteppingIterator<X> implements Iterator<X>{ private final Iterator<X> source; public EvenSteppingIterator(Iterator<X> source){ this.source = source; // skip the first one, start from the second if (source.hasNext()) source.next(); } public boolean hasNext() { return source.hasNext(); } public X next(){ X n = source.next(); // skip the next one if (source.hasNext()) source.next(); return n; } } 
+6
source

This is an illustration of inner classes, not an example of the most appropriate use of iterators.

The next thing you will complain about is that the "hello world" programs do nothing useful!

+3
source

Iterators are an abstraction. Abstractions are usually nice.

One good thing (big thing?) Is that with iterators you get the ability to analyze different data structures in a uniform way, even if the data structure cannot be indexed by an integer. For example, in C ++ you can use iterators to run arrays, sets and vectors, as well as maps and even your own data structures, all of which are conceptually and syntactically consistent. Even if the map comes from rows to widgets!

When using the C ++ approach, using iterators will always be at least as efficient as using any other access mechanism. Java has different priorities. Stephen is right that the sample code from the Java tutorial is probably not a good example of why you want to have or use iterators.

+3
source

I agree with Stephen C that this is an illustration of the inner class. I would like to point out that when you come to Java with C, you will probably notice a lot more object oriented things. Depending on your familiarity with the OO paradigm, some may seem quite alien to the functional programming style you are used to.

An inner class is just another example of turning a thing as an iterator into an object representing an iterator. By doing so, we get that level of abstraction that is of paramount importance for object-oriented programming.

BTW: Welcome to Java!

Greetings

Mike

+1
source

I do not think that I will use the inner class in the situation described in the related example. As you already mentioned, for example, it is better to use C-style code with for loops. But I think that since the intent of the textbook is to teach students how to use inner classes, an example helps to understand the concept of inner classes, although its practical use does not seem relevant.

And as mentioned in the tutorial, inner classes are useful for handling events in GUI programming. To handle events, you can register an instance of the inner class as an EventHandler (for a button action in the GUI), whose method will be called when the action is performed on the button. (Pressing the "Say" button) (callback mechanism). The inner class has access to the private members of the closing class, and if the event processing code is smaller (as in most cases) it makes sense to use inner classes with GUI code.

In my opinion, if the textbook served as an example of using an internal class with a graphical interface as an example, this would be confusing and would not serve the purpose. I think this should be a primer, and the person going through it may not be familiar with developing a GUI using Java.

0
source

Source: https://habr.com/ru/post/1306003/


All Articles