鍍金池/ 教程/ C#/ 數(shù)組
循環(huán)
正則表達式
概述
委托
多態(tài)性
字符串
繼承
結構體
集合
變量
不安全代碼
判斷
反射
異常處理
可空類型
方法
數(shù)據(jù)類型
命名空間
文件 I/O
類型轉換
屬性
程序結構
事件
接口
預處理指令
運算符
多線程
匿名方法
索引器
泛型
封裝
常量和文字
基本語法
特性
數(shù)組
環(huán)境配置
運算符重載
枚舉

數(shù)組

數(shù)組存儲一個大小固定的順序集合中相同類型的元素。數(shù)組用于存儲數(shù)據(jù)的集合,但我們通常認為數(shù)組是一個存儲在連續(xù)的內(nèi)存位置的相同類型的集合。

相反,聲明單個變量,如 number0, number1, ..., 和 number99,聲明一個數(shù)組變量,如 numbers[0], numbers[1],…, 和 numbers[99] 表示單個變量。在數(shù)組的特定元素由一個索引進行訪問。
所有數(shù)組都由連續(xù)的內(nèi)存位置構成。最低的地址對應于第一元素,最高地址為最后一個元素地址。

http://wiki.jikexueyuan.com/project/csharp/images/arrays.jpg" alt="image" />

聲明數(shù)組

要在 C# 中聲明數(shù)組,可以使用下面的語法:

datatype[] arrayName;

這里,

  • datatype 用于指定要被存儲在數(shù)組中的元素的類型
  • [ ] 指定數(shù)組的大小
  • arrayName 指定數(shù)組的名稱

例如,

double[] balance;

初始化數(shù)組

聲明沒有在存儲器初始化的數(shù)組。當數(shù)組變量初始化時,您可以賦值給數(shù)組。
數(shù)組是引用類型,所以需要使用 new 關鍵字來創(chuàng)建數(shù)組的一個實例。
例如,

double[] balance = new double[10];

賦值數(shù)組

通過使用索引號,可以將值指派給單獨的數(shù)組元素,比如:

double[] balance = new double[10];
balance[0] = 4500.0;

你可以在聲明數(shù)組的同時給它賦值,如下:

double[] balance = { 2340.0, 4523.69, 3421.0};

你也可以創(chuàng)建和初始化一個數(shù)組,如下:

int [] marks = new int[5]  { 99,  98, 92, 97, 95};

你也可以省略數(shù)組的長度,如下:

int [] marks = new int[]  { 99,  98, 92, 97, 95};

你可以將一個數(shù)組變量賦給另一個目標。這種情況,兩個數(shù)組都指向同一內(nèi)存地址。

int [] marks = new int[]  { 99,  98, 92, 97, 95};
int[] score = marks;

當你創(chuàng)建一個數(shù)組時,C# 編譯器初始化每個數(shù)組元素為數(shù)組類型的默認值。對于 int 數(shù)組的所有元素都初始化為 0。

訪問數(shù)組元素

一個元素由索引數(shù)組名訪問。這是通過放置在數(shù)組名后面的方括號里的元素索引完成的。例如:

double salary = balance[9];

以下是一個例子,將使用所有上述三個概念即聲明,分配和訪問數(shù)組:

using System;
namespace ArrayApplication
{
   class MyArray
   {
      static void Main(string[] args)
      {
         int []  n = new int[10]; /* n is an array of 10 integers */
         int i,j;

         /* initialize elements of array n */
         for ( i = 0; i < 10; i++ )
         {
            n[ i ] = i + 100;
         }

         /* output each array element's value */
         for (j = 0; j < 10; j++ )
         {
            Console.WriteLine("Element[{0}] = {1}", j, n[j]);
         }
         Console.ReadKey();
      }
   }
}

編譯運行上述代碼,得到以下結果:

Element[0] = 100
Element[1] = 101
Element[2] = 102
Element[3] = 103
Element[4] = 104
Element[5] = 105
Element[6] = 106
Element[7] = 107
Element[8] = 108
Element[9] = 109

使用 foreach 循環(huán)

在前面的例子中,我們已經(jīng)使用了一個 for 循環(huán)用于訪問每個數(shù)組元素。還可以使用 foreach 語句來遍歷數(shù)組。

using System;
namespace ArrayApplication
{
   class MyArray
   {
      static void Main(string[] args)
      {
         int []  n = new int[10]; /* n is an array of 10 integers */

         /* initialize elements of array n */
         for ( int i = 0; i < 10; i++ )
         {
            n[i] = i + 100;
         }

         /* output each array element's value */
         foreach (int j in n )
         {
            int i = j-100;
            Console.WriteLine("Element[{0}] = {1}", i, j);
            i++;
         }
         Console.ReadKey();
      }
   }
}

編譯運行上述代碼,得到以下結果:

Element[0] = 100
Element[1] = 101
Element[2] = 102
Element[3] = 103
Element[4] = 104
Element[5] = 105
Element[6] = 106
Element[7] = 107
Element[8] = 108
Element[9] = 109

數(shù)組詳解

數(shù)組在 C# 中是很重要的,應該需要很多更詳細的解釋。下列有關數(shù)組的幾個重要概念,C# 程序員應當清楚:

概念 描述
多維數(shù)組 C# 支持多維數(shù)組。多維數(shù)組的最簡單的形式是二維數(shù)組
鋸齒狀數(shù)組 C# 支持多維數(shù)組,這是數(shù)組的數(shù)組
通過數(shù)組到函數(shù) 可以通過指定數(shù)組的名稱沒有索引傳遞給函數(shù)的指針數(shù)組
參數(shù)數(shù)組 這是用于使未知數(shù)量的參數(shù)傳到函數(shù)
Array 類 定義在系統(tǒng)命名空間中,它是基類所有的數(shù)組,并使用數(shù)組提供了各種屬性和方法
上一篇:封裝下一篇:索引器