I'm running into problems trying to put a public iterator in a class to read a Map in the class and implementing this iterator in other classes. Or in other terms,
I have class A. Class A contains a private HashMap that I want to access through a public iterator that iterates this map.
I also have class B. I'm trying to run this iterator in class B to read the contents of class A.
This may seem a bit roundabout (or rather, a lot roundabout), but my assignment specified that the data system in class A be hidden to other classes and suggested using a public iterator to access the data. There is an example of what the method in class B might look like, which I've followed to the best of my ability.
Can't get it to work, though.
Here's a mockup of the code I have. I tried compiling it, and it works exactly as my real code.
// this is class A
public class Giant {
private Map<Item, Integer> myMap = new HashMap<Item, Integer>();
// add a bunch of items to the map, check if they worked fine
public Iterator<Map.Entry<Item, Integer>> giantIterator = myMap.entrySet().iterator();
}
// and this is in class B
public void receive(Giant mouse){
System.out.println("I've started!");
Iterator<Map.Entry<Item, Integer>> foo = mouse.giantIterator;
while (foo.hasNext()) {
Map.Entry<Item, Integer> entry = foo.next();
System.out.println("I'm working!");
}
}
I also have a testclass which creates objects of either class and then runs the receive method.
I receive the message "I've started!" but not "I'm working!"
At the same time, if I have the two iterators in either class print a toString, the toStrings are identical.
I can't simply move the actions I'm supposed to do in class B to class A either, because the iterator is in several different methods and is used for slightly different things in each.
I'm a bit stumped. Am I missing something from the syntax? Am I importing something wrong? Have I just completely messed up how this is supposed to work? Is this just flat out impossible?