怎么往c#函数中传入二维数组,并输出数组中的值?
/// <summary>
/// 输出二维数组
/// </summary>
/// <param name="tda">二维数组</param>
/// <returns></returns>
private string OutputTdArray(int[,] tda)
{
StringBuilder sbString = new StringBuilder(50);
int rowsCount = tda.GetLength(0);// 一维长度(行数)
int columnsCount = tda.GetLength(1); // 二维长度(列数)
int j = 0;
for (int i = 0; i < rowsCount; i++)
{
for (j=0; j < columnsCount; j++)
{
sbString.Append(tda[i, j]).Append("\t");
}
if (j % columnsCount == 0)
{
sbString.Append("\r\n");
}
}
return sbString.ToString();
}
调用:
// 传入一个4行3列的二维数组(矩阵)
string result = OutputTdArray(new int[4, 3]
{
{-2, -1, 0},
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
});
/// 输出二维数组
/// </summary>
/// <param name="tda">二维数组</param>
/// <returns></returns>
private string OutputTdArray(int[,] tda)
{
StringBuilder sbString = new StringBuilder(50);
int rowsCount = tda.GetLength(0);// 一维长度(行数)
int columnsCount = tda.GetLength(1); // 二维长度(列数)
int j = 0;
for (int i = 0; i < rowsCount; i++)
{
for (j=0; j < columnsCount; j++)
{
sbString.Append(tda[i, j]).Append("\t");
}
if (j % columnsCount == 0)
{
sbString.Append("\r\n");
}
}
return sbString.ToString();
}
调用:
// 传入一个4行3列的二维数组(矩阵)
string result = OutputTdArray(new int[4, 3]
{
{-2, -1, 0},
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
});