Today I studied Iterator pattern, but I did not quite understand a pice of code. Could you help me with it?
Here is a class:
public class Repository implements Container{
public String[] names = {"John", "Iren", "Anthony", "Lorn"};
@Override
public Iterator getIterator() {
return new MyIterator();
}
private class MyIterator implements Iterator {
int index;
@Override
public boolean hasNext() {
if (index < names.length) {
return true;
}
return false;
}
@Override
public Object next() {
if (this.hasNext()) {
return names[index++];
}
return null;
}
}
}
And the main method:
public static void main(String[] args) {
Repository name = new Repository();
for (Iterator iter = name.getIterator(); iter.hasNext(); ) {
String local = (String) iter.next();
System.out.println("Name = " + local);
}
}
The question is about method next() :
@Override
public Object next() {
if (this.hasNext()) {
return names[index++];
}
return null;
}
I don`t understand the meaning of keyword in this context. This is reference for what?