二维数组、齿形数组和游长变元表
实例学习,代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace TwoDimensionArray { class Program { public static void OutputArray(int[,] array) { for (int i = 0; i < array.GetLength(0); i++) { for (int j = 0; j < array.GetLength(1); j++) { Console.Write("{0}", array[i, j]); } Console.WriteLine(); } Console.WriteLine(); } public static void OutputJaggedArray(int[][] array) { foreach (var row in array) { foreach (var element in row) { Console.Write("{0}", element); } Console.WriteLine(); } Console.WriteLine(); } public static double ParamsArray(params double[] param) { double total = 0.0; foreach (var element in param) { total += element; } return total; } static void Main(string[] args) { int[,] rectangular = { { 1, 2, 3 }, { 4, 5, 6 } }; int[][] jagged ={new int[]{1,2}, new int[]{3}, new int[]{4,5,6}}; OutputArray(rectangular); OutputJaggedArray(jagged); double[] paraArray = new double[] { 1, 2, 3 }; Console.Write("{0}\n", ParamsArray(paraArray)); Console.ReadLine(); } } }
输出结果如下: