[英]How to declare 2D integer array in C#?
我處於初級編程階段,我想知道如何在C#中聲明一個二維數組。 我確實在Google上查了一下,但我找不到解決方案。
請在我的級別回答這個問題。
謝謝
你可以這樣做。 詳情請見此處
int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
2D整數數組
Declaration
int[,] array = new int[4, 2];
Initialization
int[,] array = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
完整的解釋與示例: http : //msdn.microsoft.com/en-us/library/2yd9wwz4.aspx
我想你還沒有搜索谷歌....
點擊以下鏈接查看教程http://www.tutorialspoint.com/csharp/csharp_multi_dimensional_arrays.htm
int [,] a = int [3,4] = {
{0, 1, 2, 3} , /* initializers for row indexed by 0 */
{4, 5, 6, 7} , /* initializers for row indexed by 1 */
{8, 9, 10, 11} /* initializers for row indexed by 2 */
};
使用MSDN進行Micrsoft技術,有很好的文檔記錄http://msdn.microsoft.com/en-us/library/2yd9wwz4.aspx
在寫作時,它在谷歌搜索中排名第一:2d整數數組c#
此頁面還可能提供有用的信息: C#中多維數組和數組數組之間有什么區別?
//二維數組
int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
//指定了維度的相同數組。
int[,] array2Da = new int[4, 2] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
//一個帶字符串元素的類似數組。
string[,] array2Db = new string[3, 2] { { "one", "two" }, { "three", "four" },
{ "five", "six" } };
//三維數組
int[, ,] array3D = new int[,,] { { { 1, 2, 3 }, { 4, 5, 6 } },
{ { 7, 8, 9 }, { 10, 11, 12 } } };
//指定了維度的相同數組。
int[, ,] array3Da = new int[2, 2, 3] { { { 1, 2, 3 }, { 4, 5, 6 } },
{ { 7, 8, 9 }, { 10, 11, 12 } } };
int[,] array2D = new int[,] { { 1, 2 }, { 3, 4 }, { 5, 6 }, { 7, 8 } };
sum2D()方法
private double sum2D(double[,] ar)
{
double sum = 0.0;
foreach (double d in ar)
sum += d;
return sum;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.