Java web server - 242 Arrays Chapter 7 Line 14 declares x

242 Arrays Chapter 7 Line 14 declares x as a reference to an array of integers. Each element in the array is of type int. The variable x is of type int[], which denotes an array whose elements are of type int. Line 15 allocates the 10 elements of the array with new and assigns the array to reference x. Each element of this array has the default value 0. Line 20 creates another int array and initializes each element using an initializer list. In this case, the number of elements in the initializer list determines the array s size. For example, line 20 creates a 10-element array with the indices 0 9 and the values 32, 27, 64, and so on. Note that this declaration does not require the new operator to create the array object the compiler allocates memory for the object when it encounters an array declaration that includes an initializer list. On line 22, we create constant integer ARRAY_SIZE using keyword const. A constant must be initialized in the same statement where it is declared and cannot be modified thereafter. If an attempt is made to modify a const variable after it is declared, the compiler issues a syntax error. Constants also are called named constants. They often are used to make a program more readable and are usually denoted with variable names in all capital letters. Common Programming Error 7.3 Assigning a value to a constant after the variable has been initialized is a compiler error. On lines 23 and 26, we create integer array z of length 10 using the ARRAY_SIZE named constant. The for structure in lines 29 30 initializes each element in array z. The values are generated by multiplying each successive value of the loop counter by 2 and adding 2 to the product. After this initialization, array z contains the even integers 2, 4, 6, , 20. The for structure in lines 35 37 uses the values in arrays x, y and z to build an output string, which will be displayed in a MessageBox. Zero-based counting (remember, array subscripts start at 0) allows the loop to access every element of the array. The constant ARRAY_SIZE in the for structure condition (line 29) specifies the arrays lengths. 7.4.2 Totaling the Elements of an Array Often, the elements of an array represent series of values to be used in calculations. For example, if the elements of an array represent the grades for an exam in a class, the professor may wish to total the elements of an array, then calculate the class average for the exam. The application in Fig. 7.4 sums the values contained in the 10-element integer array a (declared, allocated and initialized on line 12). Line 16 in the body of the for loop performs the addition using the array element at position i during each loop iteration. Note that the values being supplied as initializers for array a normally would be read into the program. For example, in a Windows application, the user could enter the values through a TextBox, or the values could be read from a file on disk. (See Chapter 17, Files and Streams.) 1 // Fig. 7.4: SumArray.cs 2 // Computing the sum of the elements in an array. 3 4 using System; 5 using System.Windows.Forms; Fig. 7.4 Computing the sum of the elements of an array. (Part 1 of 2.)
From our experience, we are can tell you that you can find a reliable and cheap webhost service at Java Web Hosting services.

Leave a Reply