using System;
class ArrayApp
{
public static void Main ( )
{
//一维数组用法:计算数组中奇偶数的个数
Console.WriteLine("一维数组演示:一维数组中的奇偶数个数");
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("共有 {0} 个偶数, {1} 个奇数。", even, odd);
//二维数组用法:m行n列的矩阵
Console.WriteLine("二维数组演示:3行4列的矩阵");
int[,] arr2 = new int[3,4] { {4,2,1,7}, {1,5,4,9}, {1,3,1,4} };
for ( int i = 0; i < 3; i++ )
{
for ( int j = 0; j < 4; j++ )
{
Console.Write(arr2[i,j] + "\t");
}
Console.WriteLine( );
}
//锯齿型数组用法:元素个数不同的两个数组
Console.WriteLine("锯齿型数组演示:长度不同的两个数组");
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("第{0}个数组是:\t",i+1);
for ( int j = 0; j < arr3[i].Length; j++ )
{
Console.Write(arr3[i][j]+ "\t");
}
Console.WriteLine();
}
}
}
|