当前位置: 代码迷 >> C# >> C#学习(4)—数组
  详细解决方案

C#学习(4)—数组

热度:10584   发布时间:2013-02-25 00:00:00.0
C#学习(四)—数组

一、数组   

       讲数组前我们先来比较一下C#的数组与VB中的数组


  数组是包含若干相同类型元素的一组变量。这些变量都可以通过索引进行访问。数组中的变量称为数组的元素。数组能够容纳元素的数量称为数组的长度。数组的维数即数组的秩。数组中的每个元素都具有唯一的索引与其相对应。数组的索引从零开始。

       数组类型是从抽象基类型Array派生的引用类型。通过new运算符创建数组并将数组元素初始化为它们的默认值。可以分为一维、多维和交错数组。

1、一维

(1)定义

  type[]arrayName ;//声明名称为x的int型数组

(2)初始化

   数据类型[]数组名= new数据类型[个数];

   int[]x = new int[10];

    初始化并赋值:int[] arr=new int[5] {1,2,3,4,5}; 

 (3)使用

   static void Main(string[] args)        {            int[] arr = new int[] { 1, 2, 3, 4, 5 };              //声明和实例化数组            Console.WriteLine("请输入一组数:");            for (int i = 0; i < 5; i++)            {                arr[i] = Convert.ToInt32(Console.ReadLine());    //类型转换            }            for (int i = 0; i < 5; i++)                          //数组长度5,0-4            {                int temp = arr[i];                               //将0对应的数值赋值给temp                int j = i;                                         //把i值赋给j                while ((j > 0) && (arr[j - 1] > temp))           //j>0且arr[j-1]>temp,执行此循环                {                    arr[j] = arr[j - 1];                         //把arr[j-1]的值赋值给arr[j],把小的数放在前面的位置                    --j;                                         //自减,直到与前面几个数比较完                }                arr[j] = temp;                                   //将temp的值赋给arr[j]            }            Console.WriteLine("排序后结果为:");            foreach (int n in arr)                                //输出结果            {                Console.WriteLine("{0}", n);            }        }

2、二维

(1)定义

       type[,]arrayName; 

(2)初始化

          int[,]arr2=new int[3,2]{{3,2},{3,4},{5,6}}; 

        二维数组初始化也是在类型说明时给各下标变量赋以初值。二维数组可按行分段赋值,也可按行连续赋值。例如对数组a[5][3]:

1.按行分段赋值可写为staticint a[5][3]={ {80,75,92},{61,65,71},{59,63,70},{85,87,90},{76,77,85} };

2.按行连续赋值可写为staticint a[5][3]={ 80,75,92,61,65,71,59,63,70,85,87,90,76,77,85 };

(3)使用

static void Main(string[] args)        {                        Console.Write("请输入定义数组的行数:");              //输入行数            int row = Convert.ToInt32(Console.ReadLine());            Console.Write("请输入定义数组的列数:");              //输入列数            int col = Convert.ToInt32(Console.ReadLine());            int[,] arr2 = new int[row, col];            Console.WriteLine("结果:");            for (int i = 0; i < row; i++)                         //  循环i小于输入的行数                            {                for (int j = 0; j < col; j++)                     //  循环j小于输入的列数                {                    if (i == j)                                  //i=j时                    {                        Console.Write("*");                      //显示*                    }                    else                                         //否则显示                    {                        Console.Write("@");                      //@                    }                }                Console.WriteLine();            }

3、多维

(1)定义

type[,(,......)]arrayName 

(2)初始化

int[, ,] arr3 = newint[2, 1, 3] {{{1,2,3}},{{4,5,6}}};

(3)使用

 int[,,] arr3; arr3=new int[,,]{{{1,2,3}},{{4,5,6}}};   foreach(int i in arr3)                //使用foreach语句遍历数组并输出      {             Console.WriteLine(i);      }

二、数组的循环

数组的循环有两种:

第一种:for循环

第一种:foreach循环

二者的区别:for循环可以不逐个遍历,foreach循环逐个遍历

博文推荐:http://blog.csdn.net/haiyan_cf/article/details/7346136

  相关解决方案