获取锯齿状数组中的列长 [英] Get Length Of Columns in Jagged Array

查看:50
本文介绍了获取锯齿状数组中的列长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a problem extracting the columns length in jagged array like this:

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

How can I get the length of the 2nd (with index 1 and length 8) or the 5th (index 4 and length 4) column for example? Same with the rows. I need length of a specified row.

推荐答案

getColumnLength方法仅遍历每一行,并检查该行是否足够长以使该列可以插入其中.如果是,则将其添加到具有该列的行数中.

The method getColumnLength just goes through each row and checks if the row is long enough for the column to be in it. If it is then add it to the count of rows that have that column.

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

这篇关于获取锯齿状数组中的列长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆