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...
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…