asp.net C#中数组常用使用方法介绍

作者:袖梨 2022-06-25

例子

 代码如下 复制代码

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();
  }
 }
}

out传递值的用法

数组为 int[] testArray = { 4, 7, 4, 2, 7, 3, 7, 8, 3, 9, 1, 9 }; 最大值为9,储存位置为9和11。

 代码如下 复制代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace Ch07Ex01
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] testArray = { 4, 7, 4, 2, 7, 3, 7, 8, 3, 9, 1, 9 };
            int[] maxValIndices;
            int maxVal = Maxima(testArray, out maxValIndices);
            Console.WriteLine("Maximum value {0} found at element indices:",
                              maxVal);
            foreach (int index in maxValIndices)
            {
                Console.WriteLine(index);
            }
            Console.ReadKey();
        }

        static int Maxima(int[] integers, out int[] indices)
        {
            Debug.WriteLine("Maximum value search started.");
            indices = new int[1];
            int maxVal = integers[0];
            indices[0] = 0;
            int count = 1;
            Debug.WriteLine(string.Format(
               "Maximum value initialized to {0}, at element index 0.", maxVal));
            for (int i = 1; i < integers.Length; i++)
            {
                Debug.WriteLine(string.Format("Now looking at element at index {0}.",
                   i));
                if (integers[i] > maxVal)
                {
                    maxVal = integers[i];
                    count = 1;
                    indices = new int[1];
                    indices[0] = i;
                    Debug.WriteLine(string.Format(
                       "New maximum found. New value is {0}, at element index {1}.",
                       maxVal, i));
                }
                else
                {
                    if (integers[i] == maxVal)
                    {
                        count++;
                        int[] oldIndices = indices;
                        indices = new int[count];
                        oldIndices.CopyTo(indices, 0);
                        indices[count - 1] = i;
                        Debug.WriteLine(string.Format(
                           "Duplicate maximum found at element index {0}.", i));
                    }
                }
            }
            Trace.WriteLine(string.Format(
               "Maximum value {0} found, with {1} occurrences.", maxVal, count));
            Debug.WriteLine("Maximum value search completed.");
            return maxVal;
        }
    }
}

使用debug.writeline,将调试结果显示在中断模式的output窗口中。

相关文章

精彩推荐