002_多维数组和锯齿状数组对比,参数数组

多维数组和锯齿状数组,写法有区别:
多维数组是这样写的:

 int[,] grades = new int[4,5];

            int[,] grades2 = new int[,] { {1,82,74,89,100 }, {2,93,96,85,86 }, {3,83,72,95,89 }, { 4, 91, 98, 79, 88 } };
            //grades[0, 0] = 4;

            //grades2.SetValue(900, 1, 1);

            int last_grade = grades2.GetUpperBound(1);
            Console.WriteLine(last_grade);

            double average = 0.0;
            int total;
            int last_student= grades2.GetUpperBound(0);

            for (int row = 0; row <= last_student; row++)
            {
                total = 0;
                for (int col = 1; col <=last_grade; col++)
                {
                    total += grades2[row,col];
                }
                average = total / last_grade;              
                Console.WriteLine(average);
            }

锯齿状数组是这样写的:

  int[][] jadded = new int[12][];

            jadded[0][0] = 3;
            jadded[0][1] = 33;

int[] Jan = new int[31];
            int[] Feb = new int[29];

            int[][] sales = new int[][] {Jan,Feb };

            int month, day, total_sales;
            double average_sales = 0.0;

            sales[0][0] = 41;
            sales[0][1] = 30;
            sales[0][2] = 23;
            sales[0][3] = 34;
            sales[0][4] = 28;
            sales[0][5] = 35;
            sales[0][6] = 37;

            sales[1][0] = 35;
            sales[1][1] = 37;
            sales[1][2] = 32;
            sales[1][3] = 26;
            sales[1][4] = 45;
            sales[1][5] = 38;
            sales[1][6] = 42;

            for ( month = 0; month <=1; month++)
            {
                total_sales = 0;
                for ( day  = 0; day  <=6; day ++)
                {
                    total_sales += sales[month][day];
                }

                average_sales = total_sales / 7;
                Console.WriteLine( "Aversge sales for month"+(month+1)+":"+average_sales);
            }
/// 
        /// 参数数组
        /// 
        /// 
        /// 
        static int sumNums(params int[] nums) {
            int sum = 0;
            for (int i = 0; i <= nums.GetUpperBound(0); i++)
            {
                sum += nums[i];
            }

            return sum;
        }

你可能感兴趣的:(002_多维数组和锯齿状数组对比,参数数组)