Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CSharp - Your Visual Blueprint For Building .NET Applications (2002) [eng].pdf
Скачиваний:
39
Добавлен:
16.08.2013
Размер:
9.71 Mб
Скачать

C#

PROGRAM ARRAY-OF-ARRAYS

The most flexible type of array is the array-of-arrays, commonly called the jagged array. The jagged array lets you define an array with several different

dimensions and sizes.

Multidimensional arrays have two or three dimensions that you can enter within the same rectangular braces that appear after the array value type. Array-of-arrays, however, let you nest single-dimensional arrays within one another. This approach lets you access a large number of arrays without the three-dimensional limit that multidimensional arrays provide.

When you initialize your array-of-arrays with the new operator, you must ensure that the number of brackets after

the new operator matches the number of brackets that appears after the array value type. If you do not, the MDE window will report the error.

Each single dimensional array must appear in its own rectangular braces that appear one after the other. You can also specify the initial element values in curly braces just as you do with single and multidimensional arrays. When you specify array values, you must ensure that the number of element values is the same as the number of arrays you specify after the array value type. For example, if you have four arrays, then you must specify four initial element values.

PROGRAM ARRAY-OF-ARRAYS

Properties

Start page appears. The New Project window

appears.

Click New Project.

Click the Console Application icon in the Templates pane.

Type a name for the file.

ˇ Click OK.

USING ARRAYS 7

Multidimensional arrays also go by the name of rectangular arrays, and if you have programmed in other languages, you may have seen these arrays referred to as ragged arrays. Microsoft has discarded the ragged moniker and has instead moved it over to the array-of-arrays corner. What is more, Microsoft changed ragged to jagged, though the change in name is only a means to set Microsoft and C# apart from other languages, because there is no change in definition from ragged to jagged.

C# refers to array-of-arrays as jagged because if you visualize the array as with in a multidimensional array, a jagged array is a series of rows for each single-dimensional array that looks like a bar chart. The height of each array bar depends on the number of elements in that array. All the bars in your array “chart” would not be of uniform height — in other words, jagged.

Á Type the code that establishes the array.

Type the code that iterates through the array and outputs the array elements that correspond with the array number.

° Run the program by

· Save the program as the

pressing the F5 key.

filename.

The jagged array elements

 

appear as members of their

 

associated array number.

 

143

C#

ITERATE THROUGH ARRAY ELEMENTS

After you program an array, you may need to iterate through array elements in case you need to list all of them for another part of your program or in your

output. C# lets you iterate through array elements by using the foreach statement.

The foreach statement is an easy way for you to display all of the elements contained in the class. The foreach statement acts as a loop that retrieves each array element. The loop follows the order that the elements appear in the array, and after the loop runs out of elements, the program moves on to the next statement.

The foreach statement appears immediately after the array statement. For example, you can view all of the elements in an array by assigning a Console.WriteLine statement after the foreach statement so you can see all of the array elements when your program runs. Another example is passing along integers from your array to a mathematical formula for further processing.

An array is a collections class that uses the System.Array base class. You can use the foreach statement for both arrays and collections. See page 150 for more information on implementing a collections class.

ITERATE THROUGH ARRAY ELEMENTS

Properties

page appears. The New Project window

appears.

Project.

Click the Console Application icon in the Templates pane.

Type a name for the file.

ˇ Click OK.

USING ARRAYS 7

You can also iterate through an array using the for statement if you want. The for statement requires you to match the array with the indexing operation whereas the foreach statement does not.

TYPE THIS:

using System; class Class1;

{

public static void Main()

{

int odd = 0, even = 0;

int[] arr = {1, 2, 3, 5, 7, 11};

for (int Index = 0; Index < arr.Count; Index++)

{

if (i%2 == 0) even++;

else odd++;

}

Class1 number = (Class1) arr[Index];

Console.WriteLine("There are {0} odd numbers and {1} even numbers.", odd, even);

}

}

RESULT:

There are 5 odd numbers and 1 even number.

Á Type the code that establishes the array.

Type the foreach argument that squares each of the elements in the array and outputs that information to the screen.

° Run the program by

· Save the program as the

pressing the F5 key.

filename.

The element square

 

information appears on the

 

screen.

 

145

Соседние файлы в предмете Программирование на C++