7.4.3 Calculating the Values to Store in an Array

The application in Fig. 7.4 creates a 10-element array and assigns to each element one of the even integers from 2 to 20 (2, 4, 6, ..., 20). Then the application displays the array in tabular format. The for statement at lines 12–13 calculates an array element’s value by multiplying the current value of the control variable counter by 2, then adding 2.

 1   // Fig. 7.4: InitArray.java 2   // Calculating the values to be placed into the elements of an array. 3  4   public class InitArray 5   { 6      public static void main(String[] args) 7      { 8         final int ARRAY_LENGTH = 10; // declare constant     9         int[] array = new int[ARRAY_LENGTH]; // create ...

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.