240 Arrays Chapter 7 The declaration int[] c (Abyss web server)

240 Arrays Chapter 7 The declaration int[] c = new int[ 12 ]; allocates 12 elements for integer array c. The preceding statement can also be performed in two steps as follows: int[] c; // declares the array c = new int[ 12 ]; // allocates the reference to the array When arrays are allocated, the elements are initialized to zero for the numeric primitive- data-type variables, to falsefor bool variables and to null for reference types. Common Programming Error 7.2 Unlike in C or C++, in C# the number of elements in the array is never specified in the square brackets after the array name. The declaration int[ 12 ] c; causes a syntax error. Memory may be reserved for several arrays with a single declaration. The following declaration reserves 100 elements for string array b and 27 elements for stringarray x: string[] b = new string[ 100 ], x = new string[ 27 ]; Similarly, the following declaration reserves 10 elements for array1and 20 elements for array2 (both of type double): double[] array1 = new double[ 10 ], array2 = new double[ 20 ]; Arrays may be declared to contain most data types. In an array of value types, every element of the array contains one value of the declared type. For example, every element of an int array is an int value. In an array of reference types, every element of the array is a reference to an object of the data type of the array. For example, every element of a stringarray is a reference to a string. Each of these string references has the value null by default. 7.4 Examples Using Arrays This section presents several examples using arrays that demonstrate declaring arrays, allocating arrays, initializing arrays and manipulating array elements in various ways. For simplicity, the examples in this section use arrays that contain elements of type int. Please remember that a program can declare arrays of most data types. 7.4.1 Allocating an Array and Initializing Its Elements Figure 7.3 creates three integer arrays of 10 elements and displays those arrays in tabular format. The program demonstrates several techniques for declaring and initializing arrays. 1 // Fig 7.3: InitArray.cs 2 // Different ways of initializing arrays. 3 4 using System; 5 using System.Windows.Forms; Fig. 7.3 Initializing element arrays in three different ways. (Part 1 of 2.)
Please visit Domain Name Hosting services for high quality webhost to host and run your jsp applications.

Leave a Reply