简体   繁体   English

c#访问二维数组中的行

[英]c# Accessing a row in a 2-dimensional array

How do I access a row of a 2-dimensional array in C#? 如何在C#中访问二维数组的行? I want to get the count of row i. 我想获得第i行的计数。

Thanks! 谢谢!

The question really depends on what type of 2D array you are thinking about and what dimension your row is. 问题实际上取决于您正在考虑的2D数组类型以及行的尺寸。

Proper 2D array 正确的2D阵列

// Init
var arr = new int[5,10];

// Counts
arr.GetLength(0) // Length of first dimension: 5
arr.GetLength(1) // Length of second dimension: 10

Jagged "2D" array 锯齿状的“ 2D”阵列

// Init
var arr = new int[3][];
// Initially arr[0],arr[1],arr[2] is null, so we have to intialize them:
arr[0] = new int[5];
arr[1] = new int[4];
arr[2] = new int[2];

// Counts
arr.Length // Length of first dimension
// In this case the "second dimension" (if you can call it that) is of variable size
arr[0].Length // Length: 5
arr[1].Length // Length: 4
arr[2].Length // Length: 2

This will also work, 这也将起作用,

string[,] a = new string[,]
{
    {"ant", "aunt"},
    {"Sam", "Samantha"},
    {"clozapine", "quetiapine"},
    {"flomax", "volmax"},
    {"toradol", "tramadol"}
};

// a[1,1] is equal to Samantha

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM