318 Arrays Chapter 7 simplicity, (Mac os x web server) the examples in
Monday, January 14th, 2008318 Arrays Chapter 7 simplicity, the examples in this section use arrays that contain elements of type int. Please remember that a program can declare an array of any data type. 7.4.1 Allocating an Array and Initializing Its Elements The application of Figure 7.3 uses operator newto allocate dynamically an array of 10 int elements, which are initially zero (the default value in an array of type int). The program displays the array elements in tabular format in a JTextArea. Line 12 declares array a reference capable of referring to an array of int elements. Line 14 allocates the 10 elements of the array with newand initializes the reference. The program builds its output in the String called output that will be displayed in a JTextArea on a message dialog. Line 16 appends to output the headings for the columns displayed by the program. The columns represent the subscript for each array element and the value of each array element, respectively. Lines 19 20 use a for structure to append the subscript number (represented by counter) and value of each array element to output. Note the use of zero-based counting (remember, subscripts start at 0), so that the loop can access every array element. Also, note the expression array.lengthin the forstructure condition to determine the length of the array. In this example, the length of the array is 10, so the loop continues executing as long as the value of control variable counteris less than 10. For a 10-element array, the subscript values are 0 through 9, so using the less than operator