I am trying to implement a depth first search for graphs using stacks. However I keep getting errors about a type variable. Here is a section of my code:
public Result<T> depthFirstSearchFrom(String vertexId, Predicate< IVertex<T> > pred){
Result<T> result = new Result<T>();
IVertex<T> startVertex = getVertex(vertexId);
Stack stack = new Stack();
stack.add(startVertex);
while (!stack.isEmpty()){
IVertex<T> current = stack.pop();
boolean visited = visitedVertices.contains(tgtVertex);
tgtVertex.getLabel().setParentVertex(current);
if (!visited){
stack.add(tgtVertex);
}
}
if (stack.isEmpty()){
result.setVisitedVertices(visitedVertices);
result.setPathCost(Float.POSITIVE_INFINITY);
}
}
return result;
}
The error occurs on the lines:
error: Incompatible types: Object cannot be converted to IVertex where T is a type variable
IVertex<T> current = stack.pop();
and
error: unchecked call to add(E) as a member of the raw type Vector where E is a type-variable.
stack.add(tgtVertex);