c#中的GetUpperBound,GetLowerBound方法


今天使用數組的時候,用到了幾個數組的屬性,總結如下:

Array的Rank 屬性:
語法:public int Rank { get; } 得到Array的秩(維數)。
Array的GetUpperBound 方法:
語法:public int GetUpperBound(int dimension) 用於獲取 Array 的指定維度的上限。
Array的GetLowerBound方法:
語法:public int GetLowerBound(int dimension) 用於獲取 Array 的指定維度的下限。
舉例如下:

int []a=new int[3];

這樣的話,a.Rank就等於1,表示這是一個一維數組。

 

string[,] Arr= new string[,]{{"1","2"},{"3","4"},{"5","6"}}; 

這樣的話,Arr.Rank就為2,表示這是一個二維數組
for(int i=Arr.GetLowerBound(0);i<=Arr.GetUpperBound(0);i++)

//Arr.GetLowerBound(0);其中的0表示取第一維的下限,一般數組索引是0開始,為0

//同理可得Arr.GetUpperBound(0);其中的0表示取第一維的上限,在本例中是3行2列的數組,所以為3-1=2
for(int j=Arr.GetLowerBound(1);j<=Arr.GetUpperBound(1);j++)
{

 

//Arr.GetLowerBound(1);其中的1表示取第二維的下限,一般數組索引是0開始,為0

//同理可得Arr.GetUpperBound(1);其中的1表示取第二維的上限,在本例中是3行2列的數組,所以為2-1=1

 

//遍歷數組的元素

Console.write(Arr[i,j]);
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM