title: Java Array Index out of Bounds Error when Array has Enough Space
body: <p>OK, so I am writing this program where part of an array is set to the value of another part of an array. The relevant code is (It is line 50 by the way):</p>
  
  <pre><code>variables[(program[1])+2]=inputs[0];
  </code></pre>
  
  <p>And here is where the array get initialized:</p>
  
  <pre><code>int[] variables;
      variables=new int[10000];
  </code></pre>
  
  <p>The program array:</p>
  
  <pre><code>int[] program;
      program=new int[10000];
  </code></pre>
  
  <p>And the inputs array:</p>
  
  <pre><code>int[] inputs;
      inputs=new int[9];
  </code></pre>
  
  <p>When I run it, it produces the error:</p>
  
  <pre><code>Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
  at Central.main(Central.java:50)
  </code></pre>
  
  <p>Any ideas? Thanks in advance.</p>
  
  <p>OK, here is the entire code:</p>
  
  <pre><code>import java.util.Random;
  </code></pre>
  
  <p>import java.util.Scanner;</p>
  
  <p>public class Central {</p>
  
  <pre><code>public static void main(String[] args) {
      int[] inputs;
      inputs=new int[9];
      int[] outputs;
      outputs=new int [9];
      int[]results;
      results=new int [10000];
      int[] program;
      program=new int[10000];
      int[][] random;
      random=new int[100][100];
      int[] bestProgram;
      bestProgram=new int[10000];
      int[] bestVariables;
      bestVariables=new int [10000];
      int[] variables;
      variables=new int[10000];
      int[][] bestRandom;
      bestRandom=new int [100][100];
       Scanner sc = new Scanner(System.in);
       inputs[0]=sc.nextInt();
       inputs[1]=sc.nextInt();
       inputs[2]=sc.nextInt();
       inputs[3]=sc.nextInt();
       inputs[4]=sc.nextInt();
       inputs[5]=sc.nextInt();
       inputs[6]=sc.nextInt();
       inputs[7]=sc.nextInt();
       outputs[0]=sc.nextInt();
       outputs[1]=sc.nextInt();
       outputs[2]=sc.nextInt();
       outputs[3]=sc.nextInt();
       outputs[4]=sc.nextInt();
       outputs[5]=sc.nextInt();
       outputs[6]=sc.nextInt();
       outputs[7]=sc.nextInt();
      program=createProgram(program);
      random=createRandom(random, program);
      for (int g=0;g&lt;=100;g++) {
      random=createRandom(random,program);
      variables=new int [program[1]+2];
      variables[program[1]+1]=1;
      results[0]=run (program,variables,random);
      variables[(program[1])+2]=inputs[0];
      results[1]=run (program,variables,random);
      variables[program[1]+2]=inputs[1];
      results[2]=run (program,variables,random);
      variables[program[1]+2]=inputs[2];
      results[3]=run (program,variables,random);
      variables[program[1]+2]=inputs[3];
      results[4]=run (program,variables,random);
      variables[program[1]+2]=inputs[4];
      results[5]=run (program,variables,random);
      variables[program[1]+2]=inputs[5];
      results[6]=run (program,variables,random);
      variables[program[1]+2]=inputs[6];
      results[7]=run (program,variables,random);
      variables[program[1]+2]=inputs[7];
      bestProgram=findBestProgram(program, variables, random, bestProgram, bestVariables, bestRandom, inputs, outputs, results);
      }
      for (int i=0;i&lt;=program[0];i++)
      {
      System.out.println(bestProgram[i]);
      }
      }
      public static int[] createProgram(int[] program) {
      program[0]=randInt(1,50);
      program[1]=randInt(1,3);
      for (int i=2;i&lt;=program[0];i++)
      {
      program[i]=randInt(1,3);
      }
      return program;
      }
      public static int run(int[] program, int[] variables,int[][] random)
      {
      for (int i=2;i&lt;=program[0];i++) {
      switch (program[i]) {
      case 1: variables[random[i][0]]=variables[random[i][1]]+variables[random[i][2]];
      break;
      case 2: variables[(random[i][0])]=(variables[(random[i][1])])-(variables[random[i][2]]);
      break;
      default: System.out.println("This error really shouldn't happen ever");
      }
      }
      return variables[0];
      }
      public static int[][] createRandom(int[][] random, int[] program)
      {
      for (int i=0;i&lt;=program[0];i++)
      {
      for (int a=0; a&lt;=2;a++)
      {
      random[i][a]=randInt(1,3);
      }
      }
      return random;
      }
      public static int[] findBestProgram(int[] program, int[] variables, int[][] random,int[] bestProgram, int[]bestVariables, int[][]bestRandom, int[] inputs, int[] outputs, int[] results)
      {
      int bestProgramResult=run(bestProgram, bestVariables,bestRandom);
      int[] resultz;
      resultz= new int [10000];
      resultz[0]=Math.abs(results[0]-outputs[0]);
      resultz[1]=Math.abs(results[1]-outputs[1]);
      resultz[2]=Math.abs(results[2]-outputs[2]);
      resultz[3]=Math.abs(results[3]-outputs[3]);
      resultz[4]=Math.abs(results[4]-outputs[4]);
      resultz[5]=Math.abs(results[5]-outputs[5]);
      resultz[6]=Math.abs(results[6]-outputs[6]);
      resultz[7]=Math.abs(results[7]-outputs[7]);
      int programResult=results[0]+results[1]+results[2]+results[3]+results[4]+results[5]+results[6]+results[7];
      if (programResult&lt;bestProgramResult)
      {
      bestProgramResult=programResult;
      for (int i=0;i&lt;=program[0];i++)
      {
      bestProgram[i]=program[i];
      bestVariables[i]=variables[i];
      bestRandom[i]=random[i];
      }
      }
      return bestProgram;
      }
      public static int randInt(int min, int max) {
          Random rand = new Random();
          int randomNum = rand.nextInt((max - min) + 1) + min;
          return randomNum;
      }
      }
      //1: _=_+_
      //2: _=_-1
  </code></pre>