250 Arrays Chapter 7 Common Programming Error 7.4 (Web design programs)

250 Arrays Chapter 7 Common Programming Error 7.4 Referring to an element outside the array bounds is a logic error. Testing and Debugging Tip 7.3 When looping through an array, the array subscript never should go below 0 and should always be less than the total number of elements in the array (one less than the length of the array). The loop-terminating condition should prevent accessing elements outside this range. Testing and Debugging Tip 7.4 Programs should validate the correctness of all input values to prevent erroneous information from affecting a program s calculations. 7.5 Passing Arrays to Methods To pass an array argument to a method, specify the name of the array without using brackets. For example, if array hourlyTemperatures declared as int[] hourlyTemperatures = new int[ 24 ]; the method call ModifyArray( hourlyTemperatures ); passes array hourlyTemperatures to method ModifyArray. Every array object knows its own size (via the Length instance variable), so when we pass an array object into a method, we do not pass the size of the array as an argument separately. Although entire arrays are passed by reference, individual array elements of primitive data types are passed by value, the same way as simple variables are. (The objects referred to by individual elements of a nonprimitive-type array are still passed by reference.) Such simple single pieces of data are sometimes called scalars or scalar quantities. To pass an array element to a method, use the subscripted name of the array element as an argument in the method call. For a method to receive an array through a method call, the method s parameter list must specify that an array will be received. For example, the method header for method ModifyArray might be written as public void ModifyArray( int[] b ) indicating that ModifyArray expects to receive an integer array in parameter b. Arrays are passed by reference; when the called method uses the array name b, it refers to the actual array in the caller (array hourlyTemperatures). The application in Fig. 7.8 demonstrates the difference between passing an entire array and passing an array element. The forloop on lines 32 33 appends the five elements of integer array ato the Text property of outputLabel. Line 33 invokes method ModifyArray and passes to it array a. Method ModifyArray multiplies each element by 2. To illustrate that array a s elements were modified, the for loop on lines 41 42 appends the five elements of integer array a to the Text property of outputLabel. As the screen capture indicates, the elements of a are modified by ModifyArray.
We recommend you use shared web hosting services, because many users agree that it is cheap, reliable and customer-satisfying webhost.

Leave a Reply