Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
401 views
in Technique[技术] by (71.8m points)

c# - Get Length Of Columns in Jagged Array

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

jagged array

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...