title: java.lang.IndexOutOfBoundsException in for each loop
body: <pre><code>public String codeGeneration() { 
  
      ArrayList&lt;String&gt; dispatchTable = new ArrayList&lt;String&gt;();
  
      if(superEntry != null) { 
          ArrayList&lt;String&gt; superDispatchTable = getDispatchTable(superEntry.getOffset());
  
          for(int i = 0; i &lt; superDispatchTable.size(); i++) {
              dispatchTable.add(i, superDispatchTable.get(i));
          }
      }
  
      String methodsCode = "";
  
      for(Node m : methods) {
          methodsCode+=m.codeGeneration();
          MethodNode mnode = (MethodNode) m;
          dispatchTable.add(mnode.getOffset(), mnode.getLabel());
      }
      addDispatchTable(dispatchTable);
  
      String codeDT = "";
      for(String s : dispatchTable) {
          codeDT+= "push " + s + "\n"
                  + "lhp\n"
                  + "sw\n" 
                  + "lhp\n"
                  + "push 1\n"
                  + "add\n"
                  + "shp\n"; 
      }
  
      return "lhp\n"
      + codeDT;
  }
  </code></pre>
  
  <p>I get the following exception:</p>
  
  <blockquote>
    <p>Exception in thread "main" java.lang.IndexOutOfBoundsException: Index:
    1, Size: 0    at java.util.ArrayList.rangeCheckForAdd(Unknown Source)
      at java.util.ArrayList.add(Unknown Source)</p>
  </blockquote>
  
  <p>The line which causes the error is: <code>dispatchTable.add(mnode.getOffset(), mnode.getLabel());</code></p>
  
  <p>Can anyone help me solve this? Thanks in advance.</p>