Java - 不兼容的类型 : Object cannot be converted to type variable (Stacks in DFS)

标签 java stack artificial-intelligence microsoft-distributed-file-system type-variables

我正在尝试使用堆栈实现图的深度优先搜索。但是我不断收到有关类型变量的错误。这是我的代码的一部分:

 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;
 }

错误发生在以下行:

错误:不兼容的类型:对象无法转换为 IVertex,其中 T 是类型变量

   IVertex<T> current = stack.pop();

错误:未经检查地调用 add(E) 作为原始类型 Vector 的成员,其中 E 是类型变量。

   stack.add(tgtVertex);

最佳答案

只需声明与 startVertex 类型相同的堆栈变量即可。

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

关于Java - 不兼容的类型 : Object cannot be converted to type variable (Stacks in DFS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42889093/

相关文章:

c++ - 将非引用变量分配给返回引用的函数,反之亦然

java - 堆栈到 --> ArrayList Java

python - 有没有人使用过语音驱动的动画,你能让它工作吗?

java - SpringBootTest @EnabledWebSocket 忽略

java - 为什么要求返回值?

Java 从 URL 读取 JSON

c# - 理解 Minimax 算法

java - 带有 servlet 的本地主机 https java 应用程序

c++ - 在我的实现中使用堆栈是否正确?

neural-network - 为什么我们在计算反向传播算法时要取传递函数的导数?