1

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!

5
  • So how many entries do you want into that array? 7 or 49? Commented Nov 23, 2013 at 1:36
  • 7. Each Vertex has an Edge[] array, and there are seven 'Edge' objects per Vertex. Commented Nov 23, 2013 at 1:37
  • So why do you have the second for loop? Commented Nov 23, 2013 at 1:39
  • Because he needs 7 vertexes with 7 edges each. The outer for loop creates the vertexes, the inner for loop creates the edges. Commented Nov 23, 2013 at 1:40
  • The first for loop is to create the Edge array for each of the seven vertices. The second for loop is to create the seven Edge objects with the specific cost of each edge. Edit: Yes ^^^ Commented Nov 23, 2013 at 1:41

1 Answer 1

1

You cannot simply instantiate something into nirvana, which is what you are trying to do in your inner loop. Besides it not being allowed to start a loop inside a static array initialization.

Create your Edge[] first, in a helper variable:

Edge[] myNewEdge = new Edge[7];
for(i=0; i<7;i++) {
    myNewEdge[i] = new Edge(...);
}

Then assign it into your vertexList:

vertexList.get(b).adjacencies = myNewEdge;
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.