Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
89 views
in Technique[技术] by (71.8m points)

java - Generic Type <T> how to use integer type in Graphs Algorithm

I want to create an adjacency matrix, but I have trouble using generic type when I get the point where the edges start and end. And I couldn't use the edges list in the Vertex class I bought out of the box and I created a edgeList myself.

This method returns the adjacency matrix:

public int[][] adjacencyMatrix() {
    int[][] adjacencyMatrix = new int[vertices.size()][vertices.size()];

    int startVertex;
    int endVertex;
    ArrayList<Edge> edgeList = new ArrayList<Edge>();
    for (int i = 0; i < vertices.size(); i++) {
        Edge currentEdge = edgeList.get(i);
        startVertex = currentEdge.from;  // <<==
        endVertex = currentEdge.to;      // <<==
        adjacencyMatrix[startVertex][endVertex] = 1;
    }
    return adjacencyMatrix;
}

Edge class:

public class Edge<T> {
    Vertex<T> from; // from which vertex the edge comes from
    Vertex<T> to; // shows which vertex goes to
    int weight;

    public Edge(Vertex<T> from, Vertex<T> to) {
        this.from = from;
        this.to = from;
        weight = 1;
    }

    public Edge(Vertex<T> from, Vertex<T> to, int weight) {
        this.from = from;
        this.to = from;
        this.weight = weight;
    }
}

Vertex class:

public class Vertex<T> {
    public T value; // storing value
    public List<Edge<T>> edges; // keeps edge list

    public Vertex(T value) {
        this.value = value;
        edges = new ArrayList<>();
    }
}
question from:https://stackoverflow.com/questions/65644092/generic-type-t-how-to-use-integer-type-in-graphs-algorithm

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Both Edge and Vertex are generic classes. Therefore you should always use them with a type parameter.

For example:

ArrayList<Edge<Integer>> edgeList = new ArrayList<>();
for (int i = 0; i < vertices.size(); i++) {
    Edge<Integer> currentEdge = edgeList.get(i);
    startVertex = currentEdge.from;  // <<==
    endVertex = currentEdge.to;      // <<==
    adjacencyMatrix[startVertex][endVertex] = 1;
}

Note that the two lines

    startVertex = currentEdge.from;  // <<==
    endVertex = currentEdge.to;      // <<==

will still not compile. That is because currentEdge.from and currentEdge.to are not Integer, but Vertex<Integer>!

To fix this you will need to write

ArrayList<Edge<Integer>> edgeList = new ArrayList<>();
for (int i = 0; i < vertices.size(); i++) {
    Edge<Integer> currentEdge = edgeList.get(i);
    startVertex = currentEdge.from.value;
    endVertex = currentEdge.to.value;
    adjacencyMatrix[startVertex][endVertex] = 1;
}

which will compile but not work as expected since your edgeList is empty...


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...