随风设计

随风设计

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  11 随笔 :: 0 文章 :: 0 评论 :: 0 Trackbacks

本教程描述数组并展示它们在 C# 中的工作方式。
教程
    本教程分为下述几节:

    1.数组概述
    2.声明数组
    3.初始化数组
    4.访问数组成员
    5.数组是对象
    6.对数组使用 foreach
数组概述
    C# 数组从零开始建立索引,即数组索引从零开始。C# 中数组的工作方式与在大多数其他流行语言中的工作方式类似。但还有一些差异应引起注意。
     声明数组时,方括号 ([]) 必须跟在类型后面,而不是标识符后面。在 C# 中,将方括号放在标识符后是不合法的语法。
    int[] table; // not int table[];
    另一细节是,数组的大小不是其类型的一部分,而在 C 语言中它却是数组类型的一部分。这使您可以声明一个数组并向它分配 int 对象的任意数组,而不管数组长度如何。
    int[] numbers; // declare numbers as an int array of any size
    numbers = new int[10]; // numbers is a 10-element array
    numbers = new int[20]; // now it's a 20-element array
声明数组
    C# 支持一维数组、多维数组(矩形数组)和数组的数组(交错的数组)。下面的示例展示如何声明不同类型的数组:

    一维数组:
     int[] numbers;

    多维数组:
    string[,] names;

    数组的数组(交错的):
    byte[][] scores;
    声明数组(如上所示)并不实际创建它们。在 C# 中,数组是对象(本教程稍后讨论),必须进行实例化。下面的示例展示如何创建数组:

    一维数组:
    int[] numbers = new int[5];

    多维数组:
    string[,] names = new string[5,4];

    数组的数组(交错的):
    byte[][] scores = new byte[5][];
    for (int x = 0; x < scores.Length; x++)
    {
      scores[x] = new byte[4];
    }

    还可以有更大的数组。例如,可以有三维的矩形数组:
    int[,,] buttons = new int[4,5,3];

    甚至可以将矩形数组和交错数组混合使用。例如,下面的代码声明了类型为 int 的二维数组的三维数组的一维数组    int[][,,][,] numbers;

C# 程序员参考--数组教程二

下面是一个完整的 C# 程序,它声明并实例化上面所讨论的数组。
    // arrays.cs
    using System;
    class DeclareArraysSample
    {
      public static void Main()
      {
         // Single-dimensional array
         int[] numbers = new int[5];

         // Multidimensional array
         string[,] names = new string[5,4];

         // Array-of-arrays (jagged array)
         byte[][] scores = new byte[5][];

         // Create the jagged array
         for (int i = 0; i < scores.Length; i++)
         {
            scores[i] = new byte[i+3];
         }

         // Print length of each row
         for (int i = 0; i < scores.Length; i++)
         {
            Console.WriteLine("Length of row {0} is {1}", i, scores[i].Length);
         }
       }
     }
输出
    Length of row 0 is 3
    Length of row 1 is 4
    Length of row 2 is 5
    Length of row 3 is 6
    Length of row 4 is 7
初始化数组
    C# 通过将初始值括在大括号 ({}) 内为在声明时初始化数组提供了简单而直接了当的方法。下面的示例展示初始化不同类型的数组的各种方法。
     注意 如果在声明时没有初始化数组,则数组成员将自动初始化为该数组类型的默认初始值。另外,如果将数组声明为某类型的字段,则当实例化该类型时它将被设置为默认值 null。

一维数组
    int[] numbers = new int[5] {1, 2, 3, 4, 5};
    string[] names = new string[3] {"Matt", "Joanne", "Robert"};
    可省略数组的大小,如下所示:

    int[] numbers = new int[] {1, 2, 3, 4, 5};
    string[] names = new string[] {"Matt", "Joanne", "Robert"};
    如果提供了初始值设定项,则还可以省略 new 运算符,如下所示:

    int[] numbers = {1, 2, 3, 4, 5};
    string[] names = {"Matt", "Joanne", "Robert"};

C# 程序员参考--数组教程三

多维数组
     int[,] numbers = new int[3, 2] { {1, 2}, {3, 4}, {5, 6} };
     string[,] siblings = new string[2, 2] { {"Mike","Amy"}, {"Mary","Albert"} };
    可省略数组的大小,如下所示:

     int[,] numbers = new int[,] { {1, 2}, {3, 4}, {5, 6} };
     string[,] siblings = new string[,] { {"Mike","Amy"}, {"Mary","Albert"} };
    如果提供了初始值设定项,则还可以省略 new 运算符,如下所示:

     int[,] numbers = { {1, 2}, {3, 4}, {5, 6} };
     string[,] siblings = { {"Mike", "Amy"}, {"Mary", "Albert"} };

交错的数组(数组的数组)
    可以像下例所示那样初始化交错的数组:

     int[][] numbers = new int[2][] { new int[] {2,3,4}, new int[] {5,6,7,8,9} };
    可省略第一个数组的大小,如下所示:

     int[][] numbers = new int[][] { new int[] {2,3,4}, new int[] {5,6,7,8,9} };
    -或-

     int[][] numbers = { new int[] {2,3,4}, new int[] {5,6,7,8,9} };
    请注意,对于交错数组的元素没有初始化语法。

访问数组成员
    访问数组成员可以直接进行,类似于在 C/C++ 中访问数组成员。例如,下面的代码创建一个名为 numbers 的数组,然后向该数组的第五个元素赋以 5:

     int[] numbers = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
     numbers[4] = 5;
    下面的代码声明一个多维数组,并向位于 [1, 1] 的成员赋以 5:

     int[,] numbers = { {1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10} };
     numbers[1, 1] = 5;
    下面声明一个一维交错数组,它包含两个元素。第一个元素是两个整数的数组,第二个元素是三个整数的数组:

     int[][] numbers = new int[][] { new int[] {1, 2}, new int[] {3, 4, 5}
     };
    下面的语句向第一个数组的第一个元素赋以 58,向第二个数组的第二个元素赋以 667:

     numbers[0][0] = 58;
     numbers[1][1] = 667;

数组是对象
    在 C# 中,数组实际上是对象。System.Array 是所有数组类型的抽象基类型。可以使用 System.Array 具有的属性以及其他类成员。这种用法的一个示例是使用“长度”(Length) 属性获取数组的长度。下面的代码将 numbers 数组的长度(为 5)赋给名为 LengthOfNumbers 的变量:

     int[] numbers = {1, 2, 3, 4, 5};
     int LengthOfNumbers = numbers.Length;
    System.Array 类提供许多有用的其他方法/属性,如用于排序、搜索和复制数组的方法。

对数组使用 foreach
    C# 还提供 foreach 语句。该语句提供一种简单、明了的方法来循环访问数组的元素。例如,下面的代码创建一个名为 numbers 的数组,并用 foreach 语句循环访问该数组:
     int[] numbers = {4, 5, 6, 1, 2, 3, -2, -1, 0};
     foreach (int i in numbers)
     {
      System.Console.WriteLine(i);
     }
    由于有了多维数组,可以使用相同方法来循环访问元素,例如:

     int[,] numbers = new int[3, 2] {{9, 99}, {3, 33}, {5, 55}};
     foreach(int i in numbers)
     {
       Console.Write("{0} ", i);
     }
    该示例的输出为:

    9 99 3 33 5 55
    不过,由于有了多维数组,使用嵌套 for 循环将使您可以更好地控制数组元素。


posted on 2006-08-17 11:31 曹贤 阅读(282) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: