簡(jiǎn)單描述C#二維數(shù)組
作者:佚名
本文介紹C#二維數(shù)組,在C#中int[][] myInt是聲明一個(gè)交錯(cuò)數(shù)組,聲明C#二維數(shù)組是這么聲明int[,] myInt。
C#有很多值得學(xué)習(xí)的地方,這里我們主要介紹C#二維數(shù)組,包括介紹聲明C#二維數(shù)組是這么聲明int[,] myInt等方面。
C#二維數(shù)組
- publicclassArray2D...{
- publicstaticvoidmain(String[]args)...{
- intmyInt[][]=newint[5][10];
- //遍歷,給數(shù)組中的每一個(gè)數(shù)組賦值
- for(inti=0;i<myInt.length;i++)...{
- for(intj=0;j<myInt[0].length;j++)...{
- myInt[i][j]=i*j;
- }
- }
- System.out.println("myInt.length="+myInt.length+",myInt[0].
length="+myInt[0].length);- //輸出數(shù)組每一維的下限和上限
- for(inti=0;i<myInt.length;i++)...{
- for(intj=0;j<myInt[0].length;j++)...{
- System.out.println("myInt["+i+"]["+j+"]="+myInt[i][j]);
- }
- }
- }
- }
在C#中int[][] myInt是聲明一個(gè)交錯(cuò)數(shù)組,聲明C#二維數(shù)組是這么聲明int[,] myInt,上面的代碼如果換成C#的,需要如下表示:
- classclsArrat2D
- {
- /**////<summary>
- ///應(yīng)用程序的主入口點(diǎn)。
- ///< SPAN>summary>
- [STAThread]
- staticvoidMain(string[]args)
- {
- int[,]myInt=newint[5,10];
- //遍歷,給數(shù)組中的每一個(gè)數(shù)組賦值
- for(inti=myInt.GetLowerBound(0);i<=myInt.GetUpperBound(0);i++)
- {
- for(intj=myInt.GetLowerBound(1);j<=myInt.GetUpperBound(1);j++)
- {
- myInt[i,j]=i*j;
- }
- }
- //輸出數(shù)組每一維的下限和上限
- for(inti=0;i<myInt.Rank;i++)
- {
- Console.WriteLine("{0}{1}{2}",i,myInt.GetLowerBound(i),myInt.GetUpperBound(i));
- }
- //遍歷,輸出二維數(shù)組中每一個(gè)元素的個(gè)數(shù)
- for(inti=myInt.GetLowerBound(0);i<=myInt.GetUpperBound(0);i++)
- {
- for(intj=myInt.GetLowerBound(1);j<=myInt.GetUpperBound(1);j++)
- {
- Console.WriteLine("myInt[{0},{1}]={2}",i,j,myInt[i,j]);
- }
- }
- Console.ReadLine();
- }
- }
以上介紹C#二維數(shù)組。
【編輯推薦】
責(zé)任編輯:佚名
來(lái)源:
51cto.com