title: Java - Add Element to an Array
body: <p>Looked around, couldn't find any similar questions in java..</p>
  
  <p>Basically I need to add a number to an int array in a specific position index</p>
  
  <p>I can only use Arrays, no <code>ArrayLists</code></p>
  
  <p>Here is what I have so far, and I know why it doesn't work, but I can't figure out how to fix that problem of overwriting, which I don't want it to do.</p>
  
  <p>The task is a non-overwriting insert. e.g. the final result would be </p>
  
  <pre><code>[1 2 1337 3 4 5 6 7 8]
  </code></pre>
  
  <p>Here is the code snippet:</p>
  
  <pre><code>public void main(String[] args)
  {
  int[] array = {1,2,3,4,5,6,7,8};
  array = add(array, 2, 1337);
  for(int i : array)
      System.out.print(i + " ");
  }
  
  
  public int[] add(int[] myArray, int pos, int n)
  {
      for (int i = pos; i&lt;myArray.length-1; i++){
          myArray[i] = myArray[i+1];
      }
      myArray[pos] = n;
      return myArray;
  }
  </code></pre>