title: Java Homework MaxSumTest
body: <p>I am currently attempting to revise a Java Homework program for my Data Structures class that displays the output of the MaxSumTest program in a table.</p>
  
  <p>I've created four arrays, and placed them inside one of the loops that calculates the timing info. I am trying to populate each array with only the timing info for one algorithm. So each array would have 4 elements. However, every time I run my revision of the program, I get an out of bounds error.</p>
  
  <p><a href="http://users.cis.fiu.edu/~weiss/dsj3/code/MaxSumTest.java" rel="nofollow">Here is the Original</a></p>
  
  <p>The only method I revised is getTimingInfo. Here is my revision:</p>
  
  <pre><code>public static void getTimingInfo( int n, int alg )
  {
      int [] test = new int[ n ];
  
      long startTime = System.currentTimeMillis( );;
      long totalTime = 0;
  
  
  
     //create an array for each Algorithm
      long[] alg4;
      long[] alg3;
      long[] alg2;
      long[] alg1;
  
      //allocate memory for 5 long ints 
  
      alg4 = new long[5];
      alg3 = new long[5];
      alg2 = new long[5];
      alg1 = new long[5];
  
     int i;
     int j;
     int index = 0;
  
     for( i = 0; totalTime &lt; 4000; i++ )
     {
         for( j = 0; j &lt; test.length; j++ )
             test[ j ] = rand.nextInt( 100 ) - 50;
      index = j;
         switch( alg )
         {
           case 1:
             maxSubSum1( test );
             break;
           case 2:
             maxSubSum2( test );
             break;
           case 3:
             maxSubSum3( test );
             break;
           case 4:
             maxSubSum4( test );
             break;
         }
  
         totalTime = System.currentTimeMillis( ) - startTime;
  
     }
  
      alg1[index] = totalTime * 1000 / i;
      alg2[index] = totalTime * 1000 / i;
      alg3[index] = totalTime * 1000 / i;
      alg4[index] = totalTime * 1000 / i;
  
      //Build first column of table
      System.out.println("Size of N Algorithms\t" + "250\t" + "2500\t" + "25000\t" + "250000");
  
      System.out.println("Alg #4\t" + alg4[0] + "\t" + alg4[1] + "\t" + alg4[2] + "\t" + alg4[3] + "\t" + alg4[4]);
      System.out.println("Alg #3\t" + alg3[0] + "\t" + alg3[1] + "\t" + alg3[2] + "\t" + alg3[3] + "\t" + alg3[4]);
      System.out.println("Alg #2\t" + alg2[0] + "\t" + alg2[1] + "\t" + alg2[2] + "\t" + alg2[3] + "\t" + alg2[4]);
      System.out.println("Alg #1\t" + alg1[0] + "\t" + alg1[1] + "\t" + alg1[2] + "\t" + alg1[3] + "\t" + alg1[4]);
  
  
      /*
  
      System.out.println( "Algorithm #" + alg + "\t"
           + "N = " + test.length
           + "\ttime = " + ( totalTime * 1000 / i ) + " microsec" );
      */
  
  
   }
  </code></pre>
  
  <p>Any advice or pointers in the right direction would be appreciated.</p>