隨風設計

          隨風設計

            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 曹賢 閱讀(301) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 湖州市| 同德县| 客服| 靖州| 肥西县| 隆尧县| 钦州市| 邮箱| 武城县| 中西区| 三穗县| 蒙城县| 银川市| 吉林省| 东兰县| 吉木萨尔县| 孝昌县| 昆明市| 新闻| 隆德县| 锦屏县| 荆门市| 自治县| 舟曲县| 普安县| 庆云县| 五原县| 博野县| 林芝县| 青川县| 宜章县| 慈溪市| 疏附县| 钟祥市| 民和| 潼南县| 沿河| 安远县| 张家口市| 大渡口区| 华池县|