1

I used this example code: https://github.com/jgrapht/jgrapht/wiki/DirectedGraphDemo to create a digraph. In this example, the Digraph is created with vertices that are Strings. I need vertices to be points, which I designate with an iD in my code (iDs are going from 0 to 3 so these are int). So I modified the example to do this:

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");

    }
}

However, I get the error "unexpected token int" at the line:

DirectedGraph<int, DefaultEdge> directedGraph =

I changed the arguments of the method to int so why am I getting this error?

rgettman
  • 176,041
  • 30
  • 275
  • 357
Graham Slick
  • 6,692
  • 9
  • 51
  • 87
  • possible duplicate of [Why don't Java Generics support primitive types?](http://stackoverflow.com/questions/2721546/why-dont-java-generics-support-primitive-types) – Andy Brown Jul 24 '15 at 17:30
  • Might be a duplicate but I had no idea that my problem could be formulated like this – Graham Slick Jul 24 '15 at 17:32

2 Answers2

4

You can't use the primitive types as generics, so change it to Integer. Autoboxing will work so you won't have to change everything else to Integer.

Bobulous
  • 12,967
  • 4
  • 37
  • 68
Arhowk
  • 901
  • 4
  • 11
  • 22
1

In Java's generics, a primitive type such as int is not allowed as a type parameter. As a substitute, you can use Java's wrapper types for primitive types. Here you can use Integer instead of int.

DirectedGraph<Integer, DefaultEdge> directedGraph = // ,...

Java's boxing/unboxing features will handle the conversion from int to Integer and back implicitly.

rgettman
  • 176,041
  • 30
  • 275
  • 357