0

I created a hashmap to represent a graph. The keys in my hash map are the vertices and the values are the edges. From my hashmap I take all of its vertices and store them in a dictionary called vertexDictionary. I would now like to set all the values of the vertex dictionary to False but I am having trouble with this. I tried to use an iterator but I am getting an error in my code with the starred line. My code is below:

public void clearMarks(){
// Sets marks for all vertices to false.
  Set set = graph.entrySet();
  Iterator i = set.iterator();

  while(i.hasNext()) {
    Map.Entry g = (Map.Entry)i.next();
    *this.graph.put(g.getKey(), false);*
  }
}
CSgirl
  • 13
  • 3

2 Answers2

0

The getKey() method returns Object. The problem is that you don't cast it. Let's say your keys are integer, then you should change your code to this:

this.graph.put((int) g.getKey(), false);
Thanasis1101
  • 1,614
  • 4
  • 17
  • 28
0

You have a vertexDictionay which contains all the vertices that you are interested in, and for each of those you want to set them to False.

So I assume that you have some data structure like Map<Integer, Boolean> vertexDictionay;

You should be able to

    for (Map.Entry vertex : vertexDictionay.entrySet()) {
        vertex.setValue(false);
    }

The foreach loop uses an iterator under the hood as discussed here. This fixes your argument mismatch error and is more readable IMHO

Nic
  • 253
  • 1
  • 3
  • 9