View Single Post
Old Jan 3rd, 2008, 9:55 AM   #7
namsu
Newbie
 
Join Date: Jan 2008
Posts: 15
Rep Power: 0 namsu is on a distinguished road
Re: Array with For Loop

I have one quick question, when the prefix increment operator is used before accessing the index of that array, what is it supposed to do, shift the index by one? Thanks for the help.

3  import java.util.Random;
 4
 5  public class RollDie
 6  {
 7     public static void main( String args[] )
 8     {
 9        Random randomNumbers = new Random(); // random number generator
10        int frequency[] = new int[ 7 ]; // array of frequency counters
11
12        // roll die 6000 times; use die value as frequency index
13        for ( int roll = 1; roll <= 6000; roll++ )
14           ++frequency[ 1 + randomNumbers.nextInt( 6 ) ];
15
16        System.out.printf( "%s%10s\n", "Face", "Frequency" );
17
18        // output each array element's value
19        for ( int face = 1; face < frequency.length; face++ )
20           System.out.printf( "%4d%10d\n", face, frequency[ face ] );
21     } // end main
22  } // end class RollDie
namsu is offline   Reply With Quote