c# - 轻松返回数组的单行

标签 c# arrays multidimensional-array indexing

<分区>

我知道我可以使用 for 和 foreach 循环 索引一个矩形数组,但我觉得我缺少一些关于如何返回数组的特定整行的语法知识。

例如,如果我的 [3,6] 数组是这样的

1 2 3 4 5 6

1 2 3 4 5 6

1 2 3 4 5 6

我只想将中间那一行打印到控制台,有没有比使用更简单的方法来实现此目的:

int[,] rectArray = new int[3, 6]
{
     {1, 2, 3, 4, 5, 6 },
     {1, 2, 3, 4, 5, 6 },
     {1, 2, 3, 4, 5, 6 }
};

Console.WriteLine("{0} {1} {2} {3} {4} {5}", 
    rectArray[0, 0], 
    rectArray[0, 1], 
    rectArray[0, 2], 
    rectArray[0, 3], 
    rectArray[0, 4], 
    rectArray[0, 5]);

我希望我可以做一些事情:

Console.Writeline(rectArray[0,]);

但由于显而易见的原因,这行不通。

最佳答案

打印中间行:

int middleRow = matrix.GetLength(0)/2;//To get the middle row index
rectArray[middleRow].ToList().ForEach(x => Console.Write(x + " "));

关于c# - 轻松返回数组的单行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34170282/

相关文章:

c# - 在生产服务器上使用 SSL 身份验证在 IMAP 上获取链状态 "RevocationStatusUnknown"

c# - 跨度和二维数组

arrays - 多维数组如何在PowerShell中工作?

c# - 检查线程是否递归运行?

c# - 如何解决 NuGet.targets(124,5) : error : Value cannot be > null. (Parameter 'folderName' ) 错误?

c# - 下面这个语句有什么问题

javascript - 通过公共(public)对象键合并多个对象数组

JavaScript 数组优化

java - 如何检查文件数据库中是否已存在电子邮件地址?

python - 向张量添加维度并沿新轴复制值