title: Insert element into a double array
body: <p>I have a method, seemingly fine, for putting double values into a double array. It is</p>
  
  <pre><code>insert(int i, double value)
  </code></pre>
  
  <p>where i is the index (array[i]) and value is what I want in that index.</p>
  
  <p>I split the method into edge cases, built in a sufficient safe block of initialized array space (length) and buffered that with a part of the method that doubles the length every time the number of elements is equal or greater than the length. I then put methods for when the input i is greater than the number of items (numItems) of the array and when it is less than numItems. i &lt; numItems is working fine, but when I try to put in </p>
  
  <pre><code>insert(63,3)
  insert(15,3)
  insert(23,3)
  </code></pre>
  
  <p>into my <code>(1,-1,5,23)</code> array I only get 2 three's on the last part of my array. My initial array length is 10, so it's not a memory issue. I thought it might be a print method error and tried to obtain the last element manually, which told me the index was empty. Thus it is a logic error in my method, which follows.</p>
  
  <pre><code>// if i is greater than the number of items, insert value into numItems index,
  // and not any farther. e.g. if i = 100000 and numItems = 10, put value into
  // items[10] and not items[100000];
  if (i &gt; numItems) 
  {
      items[numItems] = value; 
      numItems++; //add to counter
      return;
  }
  </code></pre>
  
  <p>The thing is, it's such simple code that I can't tell what's wrong with it. Very intuitive, and very puzzling. Ideas?</p>
  
  <p>below is the whole of the insert method</p>
  
  <pre><code>public void insert(int i, double value) //insert value into array[i]
  {
      if(i &lt; 0)
      {
          System.out.println("i &lt; 0; please input i &gt;= 0 for array indices."); //an array cannot have an indice &lt; 0;
          return;
      }
  
      if (numItems &gt;= items.length) // if the number of items becomes equal or greater than the array containing it
      {
          double[] tempItems = new double [items.length * 2]; // create a new array double the size of current
          for(int j =0 ; j &lt; items.length; j++ ) //and copy all elements into the new array
          {
              tempItems[j] = items[j];
          }
  
          items = tempItems; //set the temp array as the main array.
      }
  
      if (i &gt; numItems) //if i is greater than the number of items, insert value into numItems index, and not any farther.
      {                 // i.e. if i = 100000 and numItems = 10, put value into items[10] and not items[100000];
          items[numItems] = value; 
          numItems++; //add to counter
          return;
      }
  
      if ( i &lt; numItems) //if i is inside the used boundaries of the array
      {
          for (int k = numItems; k &gt; i; k--) //shift values over to the right.
          {   
              items[k]=items[k-1];
          }
  
          items[i] = value; //and insert value into i
          numItems++; //add to counter
          return;
      }
  
  
  }
  </code></pre>