C# Arrays

來源:互聯網
上載者:User
今天我要向大家講的是C#的數組(Arrays).C#中的數組和其它一些優秀的語言一樣,也是從0開始計的,這從我們以前的例子裡可以看出來,也就是說,一個數組的第一個元素是a[0],而不是像VB的a(1).雖然是這樣,但是你還是要注意一些區別.
  在聲明一個數組的時候,方括弧必須跟在類型後面,而不能跟在變數名後面,如:
  int[] table; //不能寫成int table[]
  這一點顯然與JAVA是不同的,在JAVA中這樣是可以的.
  還有就是在C#中你可以不指定數組的大小,這與C語言是不一樣的.這使得你可以指定任意長度的數組,如下:
  int[] numbers; // 它的長度是任意的
  當然,你也可以指定它的大小:
  int[10] numbers;//指定了一個長度為10的數組.
  在C#中,支援的數組包括:單維數組,多維陣列和多重數組.它們的聲明方法如下:
  單維數組: int[] numbers;
  多維陣列: string[,] names;
  多重數組: byte[][] scores;
  聲明一個數組並不代表已經建立了它.在C#中,所有的數組元素都是對象(倒!怎麼跟JAVA說得一樣&*%$#@),所以在建立它之前,首先要將它執行個體化:
  單維數組: 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[][,,][,] numbers;
下面的例子展示了以上所有構建數組的方法:
000: // Arraysarrays.cs
001: using System;
002: class DeclareArraysSample
003: {
004: public static void Main()
005: {
006: // Single-dimensional array
007: int[] numbers = new int[5];
008:
009: // Multidimensional array
010: string[,] names = new string[5,4];
011:
012: // Array-of-arrays (jagged array)
013: byte[][] scores = new byte[5][];
014:
015: // Create the jagged array
016: for (int i = 0; i < scores.Length; i++)
017: {
018: scores[i] = new byte[i+3];
019: }
020:
021: // Print length of each row
022: for (int i = 0; i < scores.Length; i++)
023: {
024: Console.WriteLine("Length of row {0} is {1}", i, scores[i].Length);
025: }
026: }
027: }
它的輸出是:
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#中數組的初始化可以在建立時就初始化,和JAVA和C一樣,用的是{}.當然,很明顯,你的初始化值必須與你聲明的數群組類型一樣,比如你定義了一個int類型的,你就不能給它一個String,唉,JAVA看多了,在C#中,String應寫為string,要不然,又要出錯了.SUNWEN可能在後面的課程中出現這樣的錯誤,還望大家指正.呵呵!
  下面的例子說明了數組的初始化:
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#中,數組的訪問和C/C++/JAVA是一樣的,下面的語句建立了一個數組,並將它的第五個元素賦值為5:
int[] numbers = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
numbers[4] = 5;
  如果你沒有C/JAVA/C++的編程經驗,那麼SUNWEN在此提醒,numbers[4]表示的是這個數組的第五個元素,因為我在前面已經說過了,數組是從0開始計的,所以0,1,2,3,4正好是第五個,所以....(台下:笨蛋,你以為我們不知道呀,快繼續說!)


  下面的例子是關於多維陣列的:
int[,] numbers = { {1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10} };
numbers[1, 1] = 5;
  再次注意,C#中的所有數組都是對象(faint,D版),所以,你可以用訪問對象的方法,來訪問數組.而System.Array就是數組的抽象.你可以參看文檔來看Array類支援的方法.舉個例子來說吧,你可以用length屬性來訪問數組的長度.如下例:
int[] numbers = {1, 2, 3, 4, 5};
int LengthOfNumbers = numbers.Length;
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.