Sea_Ocean
Sea_Ocean

Reputation: 319

How do you check that a jagged array is not multidimensional in C#

I have a matrix which is a jagged array. The matrix comes in different sizes, like the following:

char[][] grid1 = {
    new char[] { '1', '1', '0', '1', '0' },
    new char[] { '1', '1', '0', '0', '0' },
    new char[] { '0', '0', '0', '0', '0' }
};

char[][] grid2 = {
    new char[] { '1' }
};

The problem I have is that I need to loop over the matrix to get or set its elements. I do this using a nested for loop like this:

for (int i = 0; i < grid.Length; i++)
{
    for (int j = 0; j < grid[1].Length; j++)
    {
        System.Console.Write(grid[i][j]);
    }
}

The above loop works fine on grid1 because it's multidimensional. But I get an error for grid2 because it only has one dimension. The error occurs when I use "grid[1].Length". I tried "GetLength(1)" but I get the same problem because the second dimension doesn't exist.

How do I check that the matrix is multi-dimensional to avoid an exception thrown? Also is their a way to loop a jagged array regardless of how many dimensions it has?

Upvotes: 1

Views: 226

Answers (3)

Dzianis Karpuk
Dzianis Karpuk

Reputation: 146

Jagged array is always multidimensional by declaration. char[][] means you have array of arrays of chars. You can't pass char instead of char[] into char[0] element of array, it will lead to compile-time error. So we never check, if it is multidimensional, compiler makes it instead of us.

Your actual problem is using 2nd row to get Length of any row you want to access. You always need to get Lenght of row you want to access.

for (int i = 0; i < grid.Length; i++)
{
    for (int j = 0; j < grid[i].Length; j++)
    {
        System.Console.Write(grid[i][j]);
    }
}

Worst case you can deal with, when work with jagged array, is missing initialization. For example,

char[][] grid = new int[3][];

You have an array of following declaration. After this declaration you always have 3 rows, but still do not have any data in them. It is array of 3 nulls. So the previous code will not work. If you're not sure if your jagged have all your rows initialized, then you should check if it is not null.

for (int i = 0; i < grid.Length; i++)
{
    if (grid[i] != null)
    {
        for (int j = 0; j < grid[i].Length; j++)
        {
            System.Console.Write(grid[i][j]);
        }
    }
}

Upvotes: 1

Sweeper
Sweeper

Reputation: 270995

If you are sure that the jagged array forms a matrix (i.e. every subarray has the same length), you could do:

for (int i = 0; i < grid.Length; i++)
{
    // we can be sure that grid[0] always exist it ever goes into the outer loop
    for (int j = 0; j < grid[0].Length; j++)
    {
        System.Console.Write(grid[i][j]);
    }
}

But if so, you could just use a 2D array char[,], instead of a jagged array!

char[,] grid1 = {
    { '1', '1', '0', '1', '0' },
    { '1', '1', '0', '0', '0' },
    { '0', '0', '0', '0', '0' }
};

for (int i = 0 ; i < grid1.GetUpperBound(0) ; i++) {
    for (int j = 0 ; j < grid1.GetUpperBound(1) ; i++) {
        System.Console.Write(grid[i, j]);
    }
}

If each sub array can have different lengths, you can use grid[i] to access each of their lengths:

for (int i = 0; i < grid.Length; i++)
{
    for (int j = 0; j < grid[i].Length; j++)
    {
        System.Console.Write(grid[i][j]);
    }
}

Upvotes: 1

sommmen
sommmen

Reputation: 7608

EDIT: don't think this actually solves your problem, but the guy in the comments seem right, use grid[0].

for (int i = 0; i < grid.Length; i++)
{
    // Check if the current value is an array, if not display the value instead.
    if(grid[i] != typeof (char[])
    {
        System.Console.Write(grid[i]);
        continue;
    }

    for (int j = 0; j < grid[1].Length; j++)
    {
        System.Console.Write(grid[i][j]);
    }
}

Upvotes: 1

Related Questions