title: Missing something for code; ArrayList pattern with Add() and Remove() methods
body: <p>So I'm working with this online book assignment and I passed the testing phase but not the assignment. It's requesting that I use the add() and remove() methods to remove a number from a list and add another number to replace what I took out. So for instance, the original list is <code>101 200 103</code> and I have to remove <code>200</code> and add <code>100</code> and <code>102</code> to make this order: <code>100 101 102 103</code>. I was able to code to for that scenario, but now it looks like I need to make this add() and remove() method more universal. I'll show what I mean.</p>
  
  <p><strong>Directions</strong></p>
  
  <blockquote>
    <p>Modify the existing ArrayLists's contents, by erasing 200, then
    inserting 100 and 102 in the shown locations. Use ArrayList's remove()
    and add() only. Sample output of below program:</p>
    
    <p>100 101 102 103</p>
  </blockquote>
  
  <pre><code>import java.util.ArrayList;
  
  public class ArrayListADT {
  
     public static void printArray(ArrayList&lt;Integer&gt; numsList, int numOfElem) {
        int i = 0;
  
        for (i = 0; i &lt; numOfElem; ++i) {
           System.out.print(numsList.get(i));
           System.out.print(" ");
        }
  
        System.out.println("");
     }
  
     public static void main (String [] args) {
        ArrayList&lt;Integer&gt; numsList = new ArrayList&lt;Integer&gt;();
        int numOfElem = 4;
  
        numsList.add(new Integer(101));
        numsList.add(new Integer(200));
        numsList.add(new Integer(103));
  
  // CODE ABOVE THIS COMMENT CANNOT BE MODIFIED
  
        numsList.remove(new Integer(200));
        numsList.add(0, new Integer(100));
        numsList.add(2, new Integer(102));
  
  // CODE BELOW THIS COMMENT CANNOT BE MODIFIED
  
        printArray(numsList, numOfElem);
  
        return;
     }
  }
  </code></pre>
  
  <p>The code I have allows for the initial request (<code>101 200 103 -&gt; 100 101 102 103</code>) to be completed.</p>
  
  <blockquote>
    <p>Checking with numsList originally 101 200 103</p>
    
    <p>Your output:  <code>100 101 102 103</code></p>
  </blockquote>
  
  <p>However, it another test appears and fails.</p>
  
  <blockquote>
    <p>Checking with numsList originally 6 7 8 9</p>
    
    <p>Expected output: <code>100 6 102 8 9</code></p>
    
    <p>Your output: <code>100 6 102 7 8</code></p>
  </blockquote>
  
  <p>In both these tests, the original first number (<code>n0</code>) takes the place of the original second number (<code>n1</code>) and <code>n1</code> is removed from the list. Furthermore <code>100</code> and <code>102</code> appear that they need to be kept in the code as they are also tested with <code>6 7 8 9</code>. I also experimented trying <code>numsList.remove(new Integer(101));</code> and <code>numsList.remove(1, new Integer(101));</code> to the code to see what I'd get as an error. The error statement follows:</p>
  
  <blockquote>
    <p>Checking with numsList originally 6 7 8 9</p>
    
    <p>Expected output: <code>100 6 102 8 9</code></p>
    
    <p>Your output: <code>100 101 102 6 7</code></p>
  </blockquote>
  
  <p>How do I make this code fully work? I feel like it's something so simple but I'm blind to see it.</p>