我使用了以下示例代码:
https://github.com/jgrapht/jgrapht/wiki/DirectedGraphDemo创建图。在此示例中,用Strings的顶点创建了Digraph。我需要顶点作为点,我在代码中用iD指定了点(iD从0到3,所以它们是int)。因此,我修改了示例以执行此操作:

public class DirectedGraphDemo {
    public static void graph(int ... iD) {
        // constructs a directed graph with the specified vertices and edges
        DirectedGraph<int, DefaultEdge> directedGraph =
            new DefaultDirectedGraph<int, DefaultEdge>
            (DefaultEdge.class);
        directedGraph.addVertex(0);
        directedGraph.addVertex(1);
        directedGraph.addVertex(2);
        directedGraph.addVertex(3);

        directedGraph.addEdge(0,1);
        directedGraph.addEdge(1,2);
        directedGraph.addEdge(2,3);

        // computes all the strongly connected components of the directed graph
        StrongConnectivityInspector sci =
            new StrongConnectivityInspector(directedGraph);
        List stronglyConnectedSubgraphs = sci.stronglyConnectedSubgraphs();

        // prints the strongly connected components
        System.out.println("Strongly connected components:");
        for (int i = 0; i < stronglyConnectedSubgraphs.size(); i++) {
            System.out.println(stronglyConnectedSubgraphs.get(i));
        }
        System.out.println();

        // Prints the shortest path from vertex 0 to vertex 3. This certainly
        // exists for our particular directed graph.
        System.out.println("Shortest path from 0 to 3:");
        List path =
            DijkstraShortestPath.findPathBetween(directedGraph, 0, 3);
        System.out.println(path + "\n");

    }
}


但是,我在行中收到错误“意外的令牌int”:

DirectedGraph<int, DefaultEdge> directedGraph =


我将方法的参数更改为int,为什么会出现此错误?

最佳答案

您不能将原始类型用作泛型,因此请将其更改为Integer。自动装箱将起作用,因此您无需将其他所有内容都更改为Integer

关于java - 将方法参数从String更改为int时出错,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31616397/

10-10 15:16