title: 'java.lang.ArrayIndexOutOfBoundsException:Index 10 out of bounds for length 10' error?
body: <p>The above code is for inserting an element in an array. I tried changing the length of
  an array to 9 but still the issue persist.</p>
  <pre><code>public class Array1 {
      public static int[] insertX (int n,int x,int pos,int arr[]) {
          
          int newarr[] = new int[n+1];
        
          for(int i=0; i&lt;i+1;i++) {
              if(i &lt; pos -1) {
                  newarr[i]=arr[i];
              }
              else if(i==pos-1) {
                  newarr[i]=x;
              }
              else
              {
                  newarr[i]= arr[i-1];
              }
              
          }
          return newarr;
          
          
      }
      public static void main(String[] args) {
          
          int n=10;
          int arr[] = {1,2,3,4,5,6,7,8,9,10}; 
          System.out.println(&quot;Initial array&quot;+ &quot; &quot; + Arrays.toString(arr));
          int x=50;
          int pos =5;
          arr = insertX(n,x,pos,arr);
          System.out.println(&quot;Array with inserted number&quot;+ &quot; &quot; + Arrays.toString(arr));
      }
  
  }
  
   
  </code></pre>