您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“C#中二維數(shù)組的用法”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“C#中二維數(shù)組的用法”吧!
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)。 ///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(); } }
到此,相信大家對(duì)“C#中二維數(shù)組的用法”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。