CSharp 基本知識-數組

來源:互聯網
上載者:User

1.數組

CSharp 的數組從 0 開始

聲明數組時 "[ ]"放在類型名的後面,而不能放在標示符的後面,放在標示符的後面是別一種文法

如 int[] table  而不是  int table[]

另一細節是數組的大小不是其類型的一部分,而在C語言中數組的大小確是數群組類型的一部分,可以聲明一個數組

關為它分配 int (覺得這裡好像說的不對,應該是"類型")  對象的任意數組, 不管長度如何

如:

int[] table;

table=new int[10];

table=new int[20];

table= new int[30];

現在的 table 是有 30 個元素了

數組可以聲明為一維,二維,交錯數組,如

int[] num=new int[10];                   //聲明一個一維數組

int[,] num2=new int[4,5];                //聲明一個二維數組

int[][] num3=new int[3];                   //聲明一個交錯的數組

for (int x=0;x<num3.Length;x++)

{

         num3[x]=new int[4];             //相當於聲明的一個 [3,4] 的數組

}

還可以有更大的數組。例如,可以有三維的矩形數組:

甚至可以將矩形數組和交錯數組混合使用。例如,下面的代碼聲明了類型為 int 的二維數組的三維數組的一維數組。

數組的初始化

數組初始化時可以 int[] num=new int[4]{1,2,3,4}

此處注意的是初始化的元素個數得和指定 int[4] 儲存一至,否則會出錯


初始化數組
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"};
多維陣列
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 迴圈將使您可以更好地控制數組元素。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.