title: Swapping Two Objects In An ArrayList
body: <p>I am trying to swap 2 objects within an <code>ArrayList</code>. To accomplish this, I am creating a new list where the objects are swapped, then overwrite the <em>old</em> list entirely with the, <em>swapped</em> list. However, I am having trouble adding the objects from the old list to the new list.</p>
  
  <p>The program takes input from a text file, reads the data into objects (circles and rectangles, which are extensions of <code>GeometricObject</code>) and then adds those objects to an <code>ArrayList</code> called <code>objectList</code>.</p>
  
  <p>Here is the code:</p>
  
  <pre><code>public static &lt;E extends Comparable&lt;E&gt;&gt; void swapCells
  (ArrayList&lt;E&gt; objectList, int left, int right) {
  
      /* The user may enter the two indices, "left,"
       * and, "right," in any order which they desire.
       * Because of this it will be necessary to determine
       * which is larger or "right" index, and which is
       * the smaller or "left" index
       */
  
      int temp;
      ArrayList&lt;GeometricObject&gt; swappedList = new ArrayList&lt;GeometricObject&gt;();
  
      if (left &gt; right) {
  
          // Exchanges left and right
          temp = left;
          left = right;
          right = temp;
  
      }
  
      for (int i = 0; i &lt; objectList.size(); i++) {
  
          if (i == left) {
              swappedList.add(objectList.get(right));
              System.out.println( swappedList.get(i).getArea());
          } else {
              swappedList.add((E) objectList.get(i));
  
          }
  
      }
  
  } // End of swapCells
  </code></pre>
  
  <p>I get the following syntax error, and do not know what to do about it.</p>
  
  <blockquote>
    <p>The method <code>add(GeometricObject)</code> in the type <code>ArrayList&lt;GeometricObject&gt;</code> is not applicable for the arguments <code>(E)</code></p>
  </blockquote>
  
  <p>The error is specifically at, <code>swappedList.add(objectList.get(right));</code> and also <code>wappedList.add((E) objectList.get(i));</code>.</p>