Reputation: 79
I was reading through the Java tutorial on inner class
http://java.sun.com/docs/books/tutorial/java/javaOO/innerclasses.html
It explains that in the example "The InnerEvenIterator inner class, which is similar to a standard Java iterator." So I take it that iterators are pretty common in Java?
I came from a 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]));
}
got expanded to an iterators (inner class) with two methods. What's 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();
}
}
Upvotes: 1
Views: 741
Reputation: 28653
I don't think I would use an inner class in a situation outlined in the linked example. As you mention, for the example the C style code with the for loops is better. But I think since the intention of the tutorial is to educate learners about use of inner classes the example helps in understanding the concept of inner classes though its practical usage doesn't look relevant.
And as the tutorial mentions, Inner classes are useful in event handling part in GUI programming. For eventhandling you can register an instance of inner class as the EventHandler (for an action on button in the GUI) whose method will be invoked when an action is performed on the button. (Say button clicked) (callback mechanism). An inner class has access to the private members of the enclosing class and if the event handling code is smaller (as is in most of the cases) it makes sense to use inner classes with in GUI code.
In my opinion if the tutorial provided an example of inner class usage with a GUI code as an example it would have been confusing and wouldn't have served the purpose. I think it is supposed to be a beginner's tutorial and a person going through it might not have familiarity in doing GUI development using Java.
Upvotes: 0
Reputation: 262464
For simple loops over arrays, you would not (usually) use an Iterator in Java.
for(int i=0;i < arrayOfInts.length ; i+2){
System.out.println(arrayOfInts[i]));
}
The idea of an iterator is to decouple how the data is stored (might not be an array) from its consumer (the code that wants to iterate over it).
You are right to say that Iterator is a pretty core concept in the Java class library, so common that from Java5 on there is the for-each loop language feature to support it. With this loop, the user does not even see the Iterator anymore.
for(Something element: listOfSomething){
System.out.println(element);
}
If I were to implement an "even-stepping iterator" I would base it on the normal iterator, so that it can be used with any kind of 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;
}
}
Upvotes: 6
Reputation:
I agree with Stephen C that this is an illustration of an inner class. I would like to point out that coming to Java from C you will probably notice alot more object oriented things. Depending on your familiarity with the OO paradigm some may seem quite foreign compared to the functional programing style you are used to.
The inner class is simply another example of turning thing like an itterator into an object which represents an iterator. By doing this we gain that layer of abstraction which is so paramount to object oriented programming.
BTW: Welcome to Java!
Cheers,
Mike
Upvotes: 1
Reputation: 4711
Iterators are an abstraction. Abstractions are usually nice.
One nice thing (the big thing?) that having iterators gets you is the ability to examine different data structures in a uniform way, even if the data structure can't be indexed by an integer. In C++, for example, you can use iterators to run through arrays, and sets, and vectors, and maps, and even your own data structures, all in a conceptually and syntactically uniform way. Even if the map happens to be from Strings to Widgets!
With 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 code example from the Java tutorial is probably not a good example of why you'd want to have or use iterators.
Upvotes: 3
Reputation: 718688
This is an illustration of inner classes, not an example of the most appropriate use of iterators.
Next thing you'll be complaining that "hello world" programs don't do anything useful!
Upvotes: 3