Okay, I might be going crazy but I’ve never encountered this error before.
This doesn’t compile:
The error I get is : “Cannot find symbol variable visited”
EDITED
Here’s the whole function:
public void depthFirstTraverse(Node startNode) {
Stack<Node> myStack = new Stack<Node>();
myStack.push(startNode);
while (!myStack.empty()) {
Node top = myStack.pop();
top.visited = true;
System.out.println(top.item);
for (int i = 0; i < top.getAdjList().size() ; i++) {
//Node temp = (Node)top.getAdjList().get(i);
if (!(Node)top.getAdjList().get(i).visited) {
myStack.push((Node)top.getAdjList().get(i));
}
}
top.visited = false;
}
}
This part doesn’t work
if (!top.getAdjList().get(i).visited) { // this line gives me an error
This does:
for (int i = 0; i < top.getAdjList().size() ; i++) {
Node temp = (Node)top.getAdjList().get(i);
if (temp.visited) {
myStack.push(temp);
}
}
Why is this so?
if (!(Node)top.getAdjList().get(i).visited)looks like you’re trying to cast abooleanto aNodeperhaps try this instead:
if (!((Node)top.getAdjList().get(i)).visited)which does the cast then checks thevisitedproperty