3

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.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Kristian Kamenov
  • 347
  • 1
  • 3
  • 12
  • 2
    show us some code example that we can help you – BRAHIM Kamel Dec 08 '16 at 21:40
  • @Quantic he said the column length, so I think he wants to iterate through the array and count if the row contains anything for the desired column – Blake Thingstad Dec 08 '16 at 21:42
  • @BlakeThingstad Oh I see, yes the "length" of a specific column (in his picture) can't be figured out from a built in method call, you would need to iterate each sub-array to see if it contains that index, and the number of those that do is the "length" of the column. Really need to see some code so we don't mix up which index means "row" and which means "column" for OP's picture. My original comment is incorrect so I removed it. – Quantic Dec 08 '16 at 21:50

4 Answers4

5

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;
    }
}
Blake Thingstad
  • 1,639
  • 2
  • 12
  • 19
  • Thank you @Blake Thingstad! This method worked perfect! – Kristian Kamenov Dec 08 '16 at 21:58
  • I want to note that it is more [conventional](http://stackoverflow.com/questions/300522/count-vs-length-vs-size-in-a-collection) to call this a Count instead of a Length because the column elements are not necessarily contiguous. OP happens to have contiguous elements in the column direction but the number of elements is the same regardless if every row is mixed up. This is pretty minor but OP's picture makes it look like the column direction is related somehow (because every element is contiguous), but that's happenstance for a Jagged Array. Looking again Capacity may be the best fit name. – Quantic Dec 08 '16 at 22:06
  • @Quantic I hadn't thought about that actually since I've never actually worked with a jagged array, hopefully OP's array is contiguous, otherwise I wonder if there may be some errors with this. – Blake Thingstad Dec 08 '16 at 22:11
  • No I think your code is fine. I mean in your code you can swap the order of two of the arrays. Moving from `new int[] {1,3,5,7,9}, new int[] {0,2,4,6}, new int[] {11,22}` to `new int[] {1,3,5,7,9}, new int[] {11,22}, new int[] {0,2,4,6}` and now `7` and `6` are no longer contiguous (no longer touching if you drew a picture like OPs), but the "Capacity" or "Count" is still `2`. Sorry if this just adds confusion.. I feel like OP's picture makes it harder to see that there actually isn't a "Column", he has an array of arrays and each array has different lengths, "Column" doesn't make sense here. – Quantic Dec 08 '16 at 22:19
2

You should be able to get the row length fairly easily as that is simply going to be the length of an individual array in your array of arrays. For example: arrays[3].length would be 8.

Columns is different because we need information about every array. We can iterate through, counting the number of arrays whose lengths are greater than the column number you're looking for.

public static int getColumnLength(int[][] arrays, int columnNumber) {
  int count = 0;
  for(int i = 0; i < arrays.length; i++) {
    if (arrays[1].length > columnNumber) {
      count++;
    }
  }
}
ScoobyDrew18
  • 633
  • 9
  • 20
1

Use LINQ!

public static int GetColumnSize<T>(this T[][] array, int columnNumber) =>
    array
    .Where(subArray => subArray.Length > columnNumber)
    .Count();

public static int GetRowSize<T>(this T[][] array, int rowNumber) =>
    (array.Length <= rowNumber)
        ? array[rowNumber].Length
        : 0;
JamesFaix
  • 8,050
  • 9
  • 37
  • 73
-1

My task is to overflow the column for example if I am in position [2][2] ( number 9 ) and when the user types "go up with 4 steps" I need to enter from the other side of column and go to the element [4][2] ( number 15 ) on the image. I have the following method for overflowing:

static int index(int i, int n)
{
   return i % n < 0 ? n + (i % n) : i % n;
}

where i is the current position and n is the length of column/row

Kristian Kamenov
  • 347
  • 1
  • 3
  • 12
  • 3
    Consider making your code readable rather than super compact. Using proper variable names and stucture make your code much easier to understand. For example, the function name "index" doesn't tell me anything about this function, and the parameters "i" and "n" have no meaning. – Callback Kid Dec 08 '16 at 21:57
  • The method index returns the new index when my current index went out of bounds of the array. Variable 'i' is my currentIndex and variables 'n' is the length of array. – Kristian Kamenov Dec 08 '16 at 22:05
  • 1
    I wasn't asking for an explanation, just offering a piece of advice :) – Callback Kid Dec 08 '16 at 22:06
  • 1
    This is really a separate question from the original question. – JamesFaix Dec 08 '16 at 22:12