gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 19:49:48 25 4
gpt4 key购买 nike

我在像这样提取锯齿状数组中的列长度时遇到问题:

jagged array

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

最佳答案

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;
}
}

关于c# - 获取锯齿状数组中列的长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41049205/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com