|
Using System;
Class ArrayApp
{
Public static void Main ()
{
// One-dimensional array usage: calculates the number of odd and even numbers in the array
Console. WriteLine ("one-dimensional array demonstration: number of parity in one-dimensional array ");
Int [] arr1 = new int [] {8, 13, 36, 30, 9, 23, 47, 81 };
Int odd = 0;
Int even = 0;
Foreach (int I in arr1)
{
If (I % 2 = 0)
Even ++;
Else
Odd ++;
}
Console. WriteLine ("There are {0} even numbers and {1} odd numbers. ", Even, odd );
// Two-dimensional array usage: matrix of n columns in m rows
Console. WriteLine ("two-dimensional array demonstration: 3 rows and 4 columns matrix ");
Int [,] arr2 = new int }};
For (int I = 0; I <3; I ++)
{
For (int j = 0; j <4; j ++)
{
Console. Write (arr2 [I, j] + "\ t ");
}
Console. WriteLine ();
}
// Usage of a jagged array: two arrays with different numbers of elements
Console. WriteLine ("demo of a jagged array: two arrays with different lengths ");
Int [] [] arr3 = new int [2] [];
Arr3 [0] = new int [5] {1, 3, 5, 7, 9 };
Arr3 [1] = new int [4] {2, 4, 6, 8 };
// Char [] [] arr3 = new char [] [] {H, e, l, l, o}, {C, s, h, a, r, p }};
For (int I = 0; I <arr3.Length; I ++)
{
Console. Write ("The {0} array is \ t", I + 1 );
For (int j = 0; j <arr3 [I]. Length; j ++)
{
Console. Write (arr3 [I] [j] + "\ t ");
}
Console. WriteLine ();
}
}
} |