1

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);
assassinweed2
  • 71
  • 1
  • 3
  • 9
  • Firstly, you're using the raw `Stack` type - I'd advise you not to do that. Next, you're using `tgtVertex` and you haven't shown us the declaration of that. Please show a [mcve]. – Jon Skeet Mar 19 '17 at 16:38

1 Answers1

3

Just declare your stack variable of the same type of startVertex.

Stack<IVertex<T>> stack = new Stack<>();