7.4.6 Using the Elements of an Array as Counters

Sometimes, programs use counter variables to summarize data, such as the results of a survey. In Fig. 6.7, we used separate counters in our die-rolling program to track the number of occurrences of each side of a six-sided die as the program rolled the die 6,000,000 times. An array version of this application is shown in Fig. 7.7.

 1   // Fig. 7.7: RollDie.java 2   // Die-rolling program using arrays instead of switch. 3   import java.security.SecureRandom; 4  5   public class RollDie 6   { 7      public static void main(String[] args) 8      { 9         SecureRandom randomNumbers = new SecureRandom();10         int[] frequency = new int[7]; // array of frequency ...

Get Java™ How To Program (Early Objects), Tenth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.