Each object of type Vertex has an array named 'adjacencies' consisting of objects of type Edge:
class Vertex
{
public Edge[] adjacencies;
}
I have an ArrayList of Vertex objects named vertexList. Each Edge object is comprised of (vertex destination, cost to get there), with costs coming from a cost matrix:
vertexList.get(0).adjacencies = new Edge[]{ new Edge(vertexList.get(0), intMatrix[0][0]),
new Edge(vertexList.get(1), intMatrix[0][1]),
new Edge(vertexList.get(2), intMatrix[0][2]),
new Edge(vertexList.get(3), intMatrix[0][3]),
new Edge(vertexList.get(4), intMatrix[0][4]),
new Edge(vertexList.get(5), intMatrix[0][5]),
new Edge(vertexList.get(6), intMatrix[0][6])};
vertexList.get(1).adjacencies = new Edge[]{ new Edge(vertexList.get(0), intMatrix[1][0]),
new Edge(vertexList.get(1), intMatrix[1][1]),
new Edge(vertexList.get(2), intMatrix[1][2]),
new Edge(vertexList.get(3), intMatrix[1][3]),
new Edge(vertexList.get(4), intMatrix[1][4]),
new Edge(vertexList.get(5), intMatrix[1][5]),
new Edge(vertexList.get(6), intMatrix[1][6])};
This is hardcoded, which works, but with seven different vertices it is not elegant and I want to streamline the creation of these objects and arrays with loops. I tried the following with no luck:
for (int b = 0; b < 7; b++){
vertexList.get(b).adjacencies = new Edge[]{
for (int p = 0; p < 7; p++){
new Edge(vertexList.get(p), intMatrix[b][p]);
}
}
}
My IDE is telling me that the second for loop is an 'illegal start of expression' Is it possible to create the edges with a loop? Thanks!