获取锯齿状数组中列的长度

Kri*_*nov 3 c#

我在提取锯齿状数组中的列长度时遇到问题,如下所示:

锯齿状阵列

例如,如何获得第二列(索引 1 和长度 8)或第五列(索引 4 和长度 4)的长度?与行相同。我需要指定行的长度。

Bla*_*tad 5

getColumnLength 方法只是遍历每一行并检查该行是否足够长以容纳该列。如果是,则将其添加到具有该列的行数中。

public class Program {
    public static void Main(string[] args) {
        int[][] jaggedArray = {
            new int[] {1,3,5,7,9},
            new int[] {0,2,4,6},
            new int[] {11,22} 
        };

        Console.WriteLine(getColumnLength(jaggedArray, 4));

        Console.WriteLine("Press any key to continue. . .");
        Console.ReadKey();
    }

    private static int getColumnLength(int[][] jaggedArray, int columnIndex) {
        int count = 0;
        foreach (int[] row in jaggedArray) {
            if (columnIndex < row.Length) count++;
        }
        return count;
    }
}
Run Code Online (Sandbox Code Playgroud)