title: Adding node items to Java Graph with for loop
body: <p>I'm trying to add newly created objects to a grid and to a graph. Specifically, how to efficiently add nodes to a graph with a for loop. They grid has been set up as a double array, in order to update the initial view. (The object grid is the model and updates the view via push). I also set up a HashMap using the items in i and j within the for loops in order to define the object's keys. However, in order to set up a graph in order to later calculate the shortest path from node to node, I need to add these nodes to a Graph. I would like to use Djikstra's algorithm to calculate the shortest path. I could create a conditional statement to define what are corner nodes and edge nodes in the grid and define what items would have bi directional edges but this seems to be a 'long cut'. Is there any way to add nodes to a graph with a pre determined matrix size, such as for example 20 X 20, similar to a double array?</p>
  
  <p>Below is the constructor code on how I am creating the first two items (creating double array and HashMap):</p>
  
  <pre><code>// **Constructor
  // Construct a new Grid object. Sets up a HashMap of square object in order efficiently to get 
  // and add Square objects later.
  public ObjectGrid(Integer width, Integer height) 
  {
      // View
      gui = new GUI();        // Instantiate GUI
      boardView = new BoardView(width,height);        // Instantiate BoardView
  
      // Initialize Gui, set time and add simulation event listener to model (ObjectGrid)
      gui.initGUI(BoardView);
      gui.addSimulationEventListener(this);
  
      // Initialize turnInSteps variable count to 0
      turnInSteps = 0;
  
      // Initialize numberOfDays variable count to 0
      numberOfDays = 1;
  
      // Instantiate HashMap
      objectHashMap = new HashMap();
  
      // Declare new object grid using double array of type objects.
      // Size determined by parameter Integer values in constructor
      myObjectGrid = new ObjectType[width][height];
  
      // Instantiate Graph object
      Graph graph = new ListGraph();
  
      // For loop sets up the initial grid with ObejctType using a double array. After
      // the completion of this loop, the grid will have XXX objects in the grid
      // each with a reference to an object. Objects are also added 
      // to HashMap using coordinates as keys and square objects as values.
      for(int i = 0; i &lt; width; i++) 
      {  
          // Iterate through rows
          for(int j = 0; j &lt; height; j++) 
          {  
              // Iterate through columns
              myObjectGrid[i][j] = new ObjectType();  // Instantiate a new Square at each row/column using default constructor
              gridView.addObjectView(myObjectGrid[i][j].getObjectView(), i, j);  // Add object view to each row/column placement
              String hashMapKey = (i + ", " + j); // Use values from i and j as Key for objects HashMap
              myObjectGrid[i][j].setID(hashMapKey);  // Add ID's for each ObjectView to display in object using values from i and j
              objectHashMap.add(hashMapKey, myObjectGrid[i][j]);  // Add object to HashMap using string value of coordinates as key
              listGraph.add(myObjectGrid[i][j]);
  
              // Pseudo code
              if (i != (width-height) &amp;&amp; (j != height) etc) 
              {
                  listGraph.addBidirectionalEdge(mySquareGrid[i][j], (mySquareGrid[i][j+1]), 1);
                  listGraph.addBidirectionalEdge(mySquareGrid[i][j], (mySquareGrid[i+1][j]), 1);
                  listGraph.addBidirectionalEdge(mySquareGrid[i][j], (mySquareGrid[i+1][j+1]), 1);
              }
          }
      }
  }
  </code></pre>