自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

詳解C#數(shù)組的操作過程

開發(fā) 后端
本文介紹C#數(shù)組從零開始建立索引,即數(shù)組索引從零開始。C#數(shù)組的工作方式與在大多數(shù)其他流行語言中的工作方式類似。但還有一些差異應引起注意。

C#數(shù)組操作:數(shù)組概述

C#數(shù)組從零開始建立索引,即數(shù)組索引從零開始。C#數(shù)組的工作方式與在大多數(shù)其他流行語言中的工作方式類似。但還有一些差異應引起注意。

聲明數(shù)組時,方括號 ([]) 必須跟在類型后面,而不是標識符后面。在 C# 中,將方括號放在標識符后是不合法的語法。

int[] table; // not int table[];

另一細節(jié)是,數(shù)組的大小不是其類型的一部分,而在 C 語言中它卻是數(shù)組類型的一部分。這使您可以聲明一個數(shù)組并向它分配 int 對象的任意數(shù)組,而不管數(shù)組長度如何。

  1. int[] numbers; // declare numbers as an int array of any size  
  2. numbers = new int[10]; // numbers is a 10-element array  
  3. numbers = new int[20]; // now it's a 20-element array  

C#數(shù)組操作:聲明數(shù)組

C# 支持一維數(shù)組、多維數(shù)組(矩形數(shù)組)和數(shù)組的數(shù)組(交錯的數(shù)組)。下面的示例展示如何聲明不同類型的數(shù)組:

一維數(shù)組:int[] numbers;

多維數(shù)組:string[,] names;

數(shù)組的數(shù)組(交錯的):byte[][] scores;

聲明數(shù)組(如上所示)并不實際創(chuàng)建它們。在 C# 中,數(shù)組是對象(本教程稍后討論),必須進行實例化。下面的示例展示如何創(chuàng)建數(shù)組:

一維數(shù)組:int[] numbers = new int[5];

多維數(shù)組:string[,] names = new string[5,4];

數(shù)組的數(shù)組(交錯的):byte[][] scores = new byte[5][]; for (int x = 0; x <  scores.Length; x++) {scores[x] = new byt[4];

}

還可以有更大的數(shù)組。例如,可以有三維的矩形數(shù)組:int[,,] buttons = new int[4,5,3];

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

C#數(shù)組操作:初始化數(shù)組

C# 通過將初始值括在大括號 ({}) 內為在聲明時初始化數(shù)組提供了簡單而直接了當?shù)姆椒āO旅娴氖纠故境跏蓟煌愋偷臄?shù)組的各種方法。

注意 如果在聲明時沒有初始化數(shù)組,則數(shù)組成員將自動初始化為該數(shù)組類型的默認初始值。另外,如果將數(shù)組聲明為某類型的字段,則當實例化該類型時它將被設置為默認值 null。

一維數(shù)組

  1. int[] numbers = new int[5] {1, 2, 3, 4, 5};  
  2. string[] names = new string[3] {"Matt""Joanne""Robert"}; 

可省略數(shù)組的大小,如下所示:

  1. int[] numbers = new int[] {1, 2, 3, 4, 5};  
  2. string[] names = new string[] {"Matt""Joanne""Robert"}; 

如果提供了初始值設定項,則還可以省略 new 運算符,如下所示:

  1. int[] numbers = {1, 2, 3, 4, 5};  
  2. string[] names = {"Matt""Joanne""Robert"}; 

C#數(shù)組操作:多維數(shù)組

  1. int[,] numbers = new int[3, 2] { {1, 2}, {3, 4}, {5, 6} };  
  2. string[,] siblings = new string[2, 2] { {"Mike","Amy"}, {"Mary","Albert"} }; 

可省略數(shù)組的大小,如下所示:

  1. int[,] numbers = new int[,] { {1, 2}, {3, 4}, {5, 6} };  
  2. string[,] siblings = new string[,] { {"Mike","Amy"}, {"Mary","Albert"} }; 

如果提供了初始值設定項,則還可以省略 new 運算符,如下所示:

  1. int[,] numbers = { {1, 2}, {3, 4}, {5, 6} };  
  2. string[,] siblings = { {"Mike""Amy"}, {"Mary""Albert"} }; 

C#數(shù)組操作:交錯的數(shù)組(數(shù)組的數(shù)組)

可以像下例所示那樣初始化交錯的數(shù)組:

  1. int[][] numbers = new int[2][] { new int[] {2,3,4}, new int[] {5,6,7,8,9} }; 

可省略第一個數(shù)組的大小,如下所示:

  1. int[][] numbers = new int[][] { new int[] {2,3,4}, new int[] {5,6,7,8,9} };-或-  
  2. int[][] numbers = { new int[] {2,3,4}, new int[] {5,6,7,8,9} }; 

請注意,對于交錯數(shù)組的元素沒有初始化語法。

C#數(shù)組操作:訪問數(shù)組成員

訪問數(shù)組成員可以直接進行,類似于在 C/C++ 中訪問數(shù)組成員。例如,下面的代碼創(chuàng)建一個名為 numbers 的數(shù)組,然后向該數(shù)組的第五個元素賦以 5:

  1. int[] numbers = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};  
  2. numbers[4] = 5; 

下面的代碼聲明一個多維數(shù)組,并向位于 [1, 1] 的成員賦以 5:

  1. int[,] numbers = { {1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 10} };  
  2. numbers[1, 1] = 5; 

下面聲明一個一維交錯數(shù)組,它包含兩個元素。第一個元素是兩個整數(shù)的數(shù)組,第二個元素是三個整數(shù)的數(shù)組:

  1. int[][] numbers = new int[][] { new int[] {1, 2}, new int[] {3, 4, 5}}; 

下面的語句向第一個數(shù)組的第一個元素賦以 58,向第二個數(shù)組的第二個元素賦以 667:

  1. numbers[0][0] = 58;  
  2. numbers[1][1] = 667; 

C#數(shù)組操作:數(shù)組是對象

C# 數(shù)組實際上是對象。System.Array 是所有數(shù)組類型的抽象基類型??梢允褂?System.Array 具有的屬性以及其他類成員。這種用法的一個示例是使用“長度”(Length) 屬性獲取數(shù)組的長度。下面的代碼將 numbers 數(shù)組的長度(為 5)賦給名為 LengthOfNumbers 的變量:

  1. int[] numbers = {1, 2, 3, 4, 5};  
  2. int LengthOfNumbers = numbers.Length; 

System.Array 類提供許多有用的其他方法/屬性,如用于排序、搜索和復制數(shù)組的方法。

C#數(shù)組操作:對數(shù)組使用 foreach

c#數(shù)組還提供 foreach 語句。該語句提供一種簡單、明了的方法來循環(huán)訪問數(shù)組的元素。例如,下面的代碼創(chuàng)建一個名為 numbers 的數(shù)組,并用 foreach 語句循環(huán)訪問該數(shù)組:

  1. int[] numbers = {4, 5, 6, 1, 2, 3, -2, -1, 0};  
  2. foreach (int i in numbers){System.Console.WriteLine(i);} 

由于有了多維數(shù)組,可以使用相同方法來循環(huán)訪問元素,例如:

  1. int[,] numbers = new int[3, 2] {{9, 99}, {3, 33}, {5, 55}};  
  2. foreach(int i in numbers){Console.Write("{0} ", i);} 

該示例的輸出為: 9 99 3 33 5 55

不過,由于有了多維數(shù)組,使用嵌套 for 循環(huán)將使您可以更好地控制數(shù)組元素

【編輯推薦】

  1. 四種C#參數(shù)類型簡介
  2. Java與C#的不同之處
  3. 學習C#程序集
  4. C#和ADO.NET建立數(shù)據(jù)綁定網(wǎng)格
  5. 介紹C# 4.0新特性dynamic
責任編輯:book05 來源: hi.baidu
相關推薦

2011-02-24 14:23:18

2010-01-06 11:30:22

.NET Framew

2010-05-27 15:11:44

MySQL保存

2010-05-17 13:28:15

MySQL 復制

2009-12-11 17:29:22

Linux桌面

2010-03-30 12:50:42

Oracle存儲

2010-06-30 12:39:11

2010-04-14 13:18:53

安裝無線適配器

2009-12-16 17:11:10

Fedora 掛載

2010-04-26 00:42:08

DNS負載均衡

2009-10-30 10:58:45

VB.NET創(chuàng)建類

2010-03-16 15:16:01

Python web框

2010-08-18 15:42:33

2010-07-23 13:33:00

SQL Server

2010-06-01 14:17:44

MySQL重啟命令

2010-08-05 09:33:31

DB2數(shù)據(jù)庫卸載

2010-03-22 18:53:53

Python格式化字符

2009-09-02 11:02:57

C#動態(tài)數(shù)組

2009-09-02 13:15:23

C#數(shù)組復制

2009-09-02 17:07:06

C#數(shù)組操作
點贊
收藏

51CTO技術棧公眾號