title: IndexOutOfBoundException for ArrayList
body: <p><strong>My Code</strong></p>
  
  <pre><code>public List&lt;GeoPoint&gt; compute_polygon(List&lt;GeoPoint&gt; waypoints, double d)
          {
              int N = waypoints.size();//25
              int T = 2 * N - 1;//49
  //          GeoPoint[] reduced_waypoints = waypoints.toArray(new GeoPoint[waypoints.size()]);
              List&lt;GeoPoint&gt; polygon =  new ArrayList&lt;GeoPoint&gt;(T);
              for (int i = 1; i &lt; N - 1; i++)
              {
  
                  GeoPoint M = get_disp(waypoints.get(i - 1), waypoints.get(i), waypoints.get(i + 1), d);
                  polygon.add(i, v_add(waypoints.get(i), M));// here it fails and throws exception. 
                  polygon.add(T-i, v_sub(waypoints.get(i), M));
              }
  }
  </code></pre>
  
  <p><strong>Error Output</strong></p>
  
  <pre><code>Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
      at java.util.ArrayList.rangeCheck(ArrayList.java:653)
      at java.util.ArrayList.set(ArrayList.java:444)
      at de.carbook.vendor.search.PathPolygon.compute_polygon(PathPolygon.java:40)
      at de.carbook.vendor.search.Cleansing_the_waypoints.main(Cleansing_the_waypoints.java:41)
  </code></pre>
  
  <p>I am not able to figure out whats causing the problem.</p>
  
  <p>the error is at <code>polygon.add(i, v_add(waypoints.get(i), M));</code>here it fails and throws exception.</p>
  
  <p>I tried <code>polygon.add(index,element)</code> and <code>polygon.set(index,element)</code> it fails on both cases</p>
  
  <p>While debugging i found this line <code>List&lt;GeoPoint&gt; polygon =  new ArrayList&lt;GeoPoint&gt;(T);</code> is initiallized to null (Empty). Is it a problem?</p>
  
  <p><strong>Update 1</strong></p>
  
  <pre><code>public List&lt;GeoPoint&gt; compute_polygon(List&lt;GeoPoint&gt; points, double d)
          {
              int N = points.size();//25
              int T = 2 * N - 1;//49
              GeoPoint[] waypoints = points.toArray(new GeoPoint[N]);
              GeoPoint[] polygon = new GeoPoint[T];
              for (int i = 1; i &lt; N - 1; i++)
              {
                  GeoPoint M = get_disp(waypoints[i - 1], waypoints[i], waypoints[i + 1], d);
                  polygon[i]     = v_add(waypoints[i], M); 
                  polygon[T - i] = v_sub(waypoints[i], M);
              }
              GeoPoint A = v_mul(v_nor(v_prp(v_sub(waypoints[1], waypoints[0]))), d);
              GeoPoint B = v_mul(v_nor(v_prp(v_sub(waypoints[N-1], waypoints[N-2]))), d);
              polygon[0] = v_add(waypoints[0], A); polygon[T] = v_sub(waypoints[0], A);
              polygon[N - 1] = v_add(waypoints[N-1], B); polygon[N] = v_sub(waypoints[N-1], B);
  
              List&lt;GeoPoint&gt; poly =  new ArrayList&lt;GeoPoint&gt;(Arrays.asList(polygon));
              System.out.println(poly);
  
              return poly;
          }
  </code></pre>
  
  <p><strong>Error for update</strong></p>
  
  <pre><code>Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 49
      at de.carbook.vendor.search.PathPolygon.compute_polygon(PathPolygon.java:67)
      at de.carbook.vendor.search.Cleansing_the_waypoints.main(Cleansing_the_waypoints.java:41)
  </code></pre>