274 Arrays Chapter 7 In an array
274 Arrays Chapter 7 In an array of primitive data types, every element of the array contains one value of the declared data type of the array. In an array of a reference type, every element of the array is a reference to an object of the data type of the array. For example, every element of a string array is a reference to a stringand that reference has the value null by default. The elements of single-dimensional and rectangular arrays can be allocated and initialized in the array declaration by following the declaration with an equal sign and a comma-separated initializer list enclosed in braces ({ and }). A const variable must be declared and initialized in the same statement. Constants also are called named constants. They often are used to make a program more readable. Unlike its predecessors C and C++, .NET-compliant languages provide mechanisms to prevent accessing elements outside the bounds of the array. When a reference is made to a nonexistent element of an array, an IndexOutOfRangeException occurs. To pass an array argument to a method, specify the name of the array without any brackets. Although entire arrays are passed by reference, individual array elements of primitive data types are passed by value, as are simple variables. To pass an array element to a method, use the subscripted name of the array element as an argument in the method call. Sorting data (i.e., placing the data into a particular order, such as ascending or descending) is one of the most important computing applications. The chief virtue of the bubble sort is that it is easy to program. However, the bubble sort runs slowly, which becomes apparent when sorting large arrays. The linear search method works well for small or unsorted arrays. However, for large arrays, linear searching is inefficient. After each comparison, the binary search algorithm eliminates from consideration half the elements in the array being searched. The algorithm locates the middle array element and compares it to the search key. If they are equal, the search key has been found, and the subscript of that element is returned. Otherwise, the problem is reduced to searching half the array. If the search key is less than the middle array element, the first half of the array is searched; otherwise, the second half of the array is searched. The search continues until the search key is equal to the middle element of a subarray, or until the subarray consists of one element that is not equal to the search key (i.e., the search key is not found). The maximum number of comparisons needed for the binary search of any sorted array is the exponent of the first power of 2 that is greater than the number of elements in the array. There are two types of multiple-subscripted arrays rectangular and jagged. In general, an array with m rows and n columns is referred to as an m-by-n array. Multiple-subscripted arrays can be initialized in declarations, as can single-subscripted arrays. The compiler determines the number of columns in each row by counting the number of initializer values in the sub-initializer list for that row. Jagged arrays are maintained as arrays of arrays. Unlike rectangular arrays, rows in jagged arrays can be of different lengths. Many common array manipulations use for repetition structures. When used with one-dimensional arrays, foreach behaves like a for structure that iterates through the range of indices from 0 to the array s Length.
We recommend cheap and reliable webhost to host and run your web applications: Coldfusion Web Hosting services.