I'm trying to get my mind around the Iterator functionality in Java through some examples, and I can't figure out how to access the iterator so that I can start printing out the collection.
So I have this function:
class A <Item> implements Iterable <Item> {
//....does stuff
}
public Iterator <Item> iterator() {
return new Itor();
}
class Itor implements Iterator <Item> {
//.....custom Iterator functions within
}
Basically, class A is a data structure that holds a list of values, and I want to print these values out using iterator from within another class. How would I implement that? I'm not sure on how the syntax would look like.
Let's say I have another class
class B {
private class Node {
A<Integer> n; // I have another class that inserts values into the A class
// and I want to be able to print n out.
}
private void printA() {
Iterator it = A.iterator(); //I get the non-static method error here.
}
}
Figured it out, it was something as simple as