|
數(shù)組是一種存儲相同類型元素的固定大小順序集合。數(shù)組用于存儲數(shù)據(jù)集合,但一般會將數(shù)組視為存儲在連續(xù)存儲器位置的相同類型的變量的集合。 如果要存儲表示100名稱學(xué)生的分?jǐn)?shù),需要獨(dú)立地聲明100整數(shù)變量。例如:number0.number1...., number100這樣單個獨(dú)立變量。而如果使用一個數(shù)組變量來表示就省事多了。例如,首先聲明數(shù)組:numbers,使用numbers[0],numbers[1]和...,numbers[99]來表示單個變量,數(shù)組中的元素可通過索引來訪問。 所有數(shù)組是由連續(xù)的內(nèi)存位置組成。最低的地址對應(yīng)于第一個元素,而最后一個元素的地址最高。 聲明數(shù)組要在 C# 中聲明一個數(shù)組,可以使用以下語法: datatype[] arrayName;1復(fù)制代碼類型:[csharp] 其中 : datatype : 用于指定數(shù)組中元素的類型。 [] : 指定數(shù)組序號,rank指定數(shù)組的大小。 arrayName : 指定數(shù)組的名稱。 例如, double[] balance;1復(fù)制代碼類型:[csharp] 初始化數(shù)組聲明數(shù)組不會將的數(shù)組初始化到內(nèi)存中。將數(shù)組變量初始化時(shí),可以為數(shù)組指定值。 數(shù)組是一個引用類型,因此需要使用new關(guān)鍵字來創(chuàng)建數(shù)組的實(shí)例。 例如, double[] balance = new double[10];1復(fù)制代碼類型:[csharp] 數(shù)組賦值可以通過使用索引數(shù)為各個數(shù)組元素分配值,如: double[] balance = new double[10]; balance[0] = 1500.0; balance[1] = 1000.0; balance[2] = 2000.0;1234復(fù)制代碼類型:[csharp] 也可以在聲明時(shí)為數(shù)組指定值,如下所示: double[] balance = { 240.08, 523.19, 121.01};12復(fù)制代碼類型:[csharp]還可以在創(chuàng)建時(shí)初始化數(shù)組,如下所示: int [] marks = new int[5] { 89, 98, 97, 87, 85};12復(fù)制代碼類型:[csharp]也可以省略數(shù)組的大小,如下所示: int [] marks = new int[] { 100, 97, 96, 97, 95};1復(fù)制代碼類型:[csharp]可以將數(shù)組變量復(fù)制到另一個目標(biāo)數(shù)組變量中。在這種情況下,目標(biāo)和源都指向相同的內(nèi)存位置: int [] marks = new int[] { 99, 98, 92, 97, 95};int[] score = marks;12復(fù)制代碼類型:[csharp]創(chuàng)建數(shù)組時(shí), C# 編譯器會根據(jù)數(shù)組類型將每個數(shù)組元素初始化為默認(rèn)值。 例如,對于int類型的數(shù)組,所有元素都將初始化為0. 訪問數(shù)組元素通過索引和數(shù)組名稱來訪問數(shù)組的元素。這是通過將元素的索引放在數(shù)組的名稱后面的方括號內(nèi)完成的。 例如, double salary = balance[9];1復(fù)制代碼類型:[csharp] 以下示例演示了如何聲明,賦值和訪問數(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();
}
}
}12345678910111213141516171819202122232425復(fù)制代碼類型:[csharp]當(dāng)編譯和執(zhí)行上述代碼時(shí),會產(chǎn)生以下結(jié)果: Element[0] = 100Element[1] = 101Element[2] = 102Element[3] = 103Element[4] = 104Element[5] = 105Element[6] = 106Element[7] = 107Element[8] = 108Element[9] = 10912345678910復(fù)制代碼類型:[csharp] 使用foreach循環(huán)在前面的例子中,我們使用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);
}
Console.ReadKey();
}
}
}1234567891011121314151617181920212223242526復(fù)制代碼類型:[csharp]當(dāng)編譯和執(zhí)行上述代碼時(shí),會產(chǎn)生以下結(jié)果: Element[0] = 100Element[1] = 101Element[2] = 102Element[3] = 103Element[4] = 104Element[5] = 105Element[6] = 106Element[7] = 107Element[8] = 108Element[9] = 10912345678910復(fù)制代碼類型:[csharp] C# 數(shù)組類型C# 程序員應(yīng)該要清楚以下幾個與數(shù)組有關(guān)的重要概念:
|
|
|