我想创建一个邻接矩阵,但是当我得到边开始和结束的点时,我在使用泛型类型时遇到了问题。我无法使用列表中的边 Vertex
我买了一个开箱即用的类,我创建了一个 edgeList
我自己。
此方法返回邻接矩阵:
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` 班级:
public class Edge {
Vertex from; // from which vertex the edge comes from
Vertex 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
班级:
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<>();
}
}
暂无答案!
目前还没有任何答案,快来回答吧!