C#3.0學習筆記(6)詳談數組

來源:互聯網
上載者:User

1,  數組的定義及重要細節?

  定義:數組是由一個變數名稱表示的一組同類型的資料元素,每個元素通過變數名稱和一個或多個方括弧的索引名稱來訪問。

  注意:1> 數組一旦被建立,大小就固定了。C#不支援動態數組。

           2> 數組索引號從0開始。也就是說如果維度長度是n,索引號範圍是從0到n-1。

           3> 數組屬於參考型別,即引用在棧上或堆上,但是數組對象本身總是在堆上。

2,  數組的分類?

  1>     一維數組

  2>     矩形數組(即某個維度所有子數組有相同長度)

  3>     交錯數組s(即可以有不同長度的子數組)

3,  數組的聲明,初始化和遍曆數組元素?

  一維數組:

namespace array1
{
class Program
{
static void Main(string[] args)
{
//int[] arr1;//聲明數組
//arr1 = new int[] { 10, 20, 30, 40, 50 };//初始化數組
int[] arr1 = new int[] { 10, 20, 30, 40, 50 };//聲明和初始化可以一步完成。等價於:int[] arr1 = { 10, 20, 30, 40, 50 };//快捷文法
//var arr1 = new[] {10, 20, 30, 40, 50 }; //隱式類型數組。

//遍曆數組
//方法一:使用for迴圈:
for (int i = 0; i < arr1.Length; i++)
{
Console.WriteLine("數組元素{0}的值為:{1}",i,arr1[i]);
}
//方法二:使用foreach語句:
//foreach (int item in arr1)//迭代變數item表示數組中的每一個元素。
//{
// Console.WriteLine("元素的值為:{0}", item);
//}
Console.ReadKey();
}
}
}

  程式輸出結果為:


  二維數組(矩形數組):

namespace array2
{
class Program
{
static void Main(string[] args)
{
int[,] arr2 = new int[,] { {0,1,2},{10,11,12}};//聲明和初始化二維數組。
//遍曆數組
//方法一:使用for迴圈:
//for (int i = 0; i < 2; i++)
//{
// for (int j = 0; j < 3; j++)
// {
// Console.WriteLine("元素[{0},{1}]的值為:{2}",i,j,arr2[i,j]);
// }
//}
//方法二:使用foreach語句:
foreach (int item in arr2) //對於多維陣列,元素的處理次序是最右邊的索引號最先遞增的,當索引從0到長度減1時,下一個左邊的索引被遞增,右邊的索引被重設為0.
{
Console.WriteLine("元素的值分別為:{0}",item);
}
Console.ReadKey();
}
}
}

  程式輸出結果為:

 

         交錯數組:

namespace arr3
{
class Program
{
static void Main(string[] args)
{
int[][] arr3 = new int[2][];
/*交錯數組的執行個體化過程如下:
首先,執行個體化頂層數組。
其次,分別執行個體化每一個子數組,把建立數組的引用賦值給包含它們的數組的合適元素
*/
arr3[0] = new int[] { 10,11};
arr3[1] = new int[] { 12,13,14};
//遍曆數組:
foreach (int[] array in arr3)
{
Console.WriteLine("Starting new array");
foreach (int item in array)
{
Console.WriteLine("數組的元素分別為:{0}",item);
}
}
Console.ReadKey();
}
}
}

   程式輸出結果為:

 

4, 數組的一些重要屬性和方法總結?

namespace array4
{
class Program
{
static void Main(string[] args)
{
int[] arr4 = new int[] { 15,20,5,25,10};
PrintArray(arr4);
Console.WriteLine();//換行

Array.Sort(arr4);//方法Sort,排序,預設為升序
PrintArray(arr4);
Console.WriteLine();

Array.Reverse(arr4);//方法Reverse,反轉
PrintArray(arr4);
Console.WriteLine();
Console.WriteLine();

Console.WriteLine("維度為:{0},長度為:{1}", arr4.Rank, arr4.Length);//屬性Rank表示數組維度,屬性Length表示數組長度。
Console.WriteLine("GetLength(0)={0}", arr4.GetLength(0));//方法GetLength()表示返回數組中指定維度長度。
Console.WriteLine("GetType()={0}", arr4.GetType());//方法GetType()表示返回數群組類型。
Console.ReadKey();

}
protected static void PrintArray(int[] a)
{
foreach (int item in a)
{
Console.Write("{0}", item);
Console.Write("");//在每個數組元素後面加一空格。
}
}
}
}

   程式輸出結果為:

哎,很晚了,要睡覺了,先總結到這兒吧,不足的部分請大家補充補充啊!

相關文章

聯繫我們

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