0

i have a huge Problem when dealing with jagged arrays [][].

I wrote a program that interacts with lots of CSV-files. It will read them and then compare them. Now i have a problem if Array A has the dimension of 10 Rows and 10 Columns but Array B only has the dimension of 5 Rows and 5 Columns. I get the "out of range" on array B. This is only an example it gets even worse if i have a array which has different amount of Rows in each Column...

I tried checking for "null" but this doesnt work since i get the "out of range" once it tries to acess the field...

Now i have 2 theories to solve the problem:

A.)Check for "out of range" in Array B and if so fill Array A at the same field with a "0"

B.) Check if Array A and Array B has same dimension and if not fill the array with lesser amount with "0" so that it has the same amount

On both solutions i have absolutely no clue how to do this in C#... I am always getting the out of range...

What i currently do for 1 array is:

for (int b = CSV_Statistiken.Length - 1; b >= 0; b--)   
{
    for (int a = 0; a < CSV_Statistiken[b].Length; a++)     
    {
        CSV_Statistiken[b][a] = 1;
    }
}

so i get the dimension of the array and iterate through it, setting every value to 1. But how do i deal with my problem with 2 arrays?

I researched a bit but couldnt find any solution to this =/

Thanks in advance

Edit: What i am trying to do for examlple:

for (int i = 0; i < number; i++) //runs through every File existing
{
    NextFile = fold.Filepath + "\\" + files[i].ToString();
    file = new FileInfo(@NextFile);
    max_Rows = 0;
    max_Col = 0;
    CSV_temp = ReadCSV(file, ref max_Rows, ref max_Col); // reads the next file to an arraay [][] and saves the size of this array in max_col/ max_rows

    MAX_Col_Total = GetHighestValues(ref MAX_Col_Total, max_Col);
    MAX_Rows_Total = GetHighestValues(ref MAX_Rows_Total, max_Rows);

    for (int j = 0; j < MAX_Col_Total; j++)      //runs thrugh the max amount of cols found
    {
        for (int k = MAX_Rows_Total - 1; k >= 0; k--)   //runs through the max mount of rows found
        {
             if (CSV_temp.GetLength(0) >= j && CSV_temp.GetLength(1) >= k)//Checks if Field exists -> does NOT work!
             {
                 if (CSV_temp[k][j] > (Threshhold))) //   
                 {
                     do something
                 }
             }
             else
             {
                 // Field doesnt exists -> do something else
             }
        }
    }
}
Roman
  • 11,966
  • 10
  • 38
  • 47
christian890
  • 147
  • 1
  • 12

1 Answers1

3

You can check Lengths of two arrays in for loops:

for (int a = 0; a < array1.Length && a < array2.Length; a++)   
{
    for (int b = 0; b < array1[a].Length && b < array2[a].Length; b++)     
    {
        //compare
    }
}

Now your loops never go outside of any array index and you won't get IndexOutOfRangeException.

EDIT:

var biggestLength1 = Math.Max(array1.Length, array2.Length);   

for (int a = 0; a < biggestLength1; a++)   
{
    var biggestLength2 = 0;

    if (array1.Length > a && array2.Length > a)
    {
        biggestLength2 = Math.Max(array1[a].Length, array2[a].Length);
    }
    else
    {
        biggestLength2 = array1.Length > a ? array1.Length : array2.Length;
    }

    for (int b = 0; b < biggestLength2; b++)     
    {
        if (a < array1.Length && 
            a < array2.Length && 
            b < array1[a].Length && 
            b < array2[a].Length)
        {
            // every array has enough elements count
            // you can do operations with both arrays
        }
        else
        {
            // some array is bigger                           
        }
    }
}
Roman
  • 11,966
  • 10
  • 38
  • 47
  • Okay this isnt too bad, i wont get the outofrange anymore, BUT ill skip lots of Fields =/ I rather want the biggest Array to dominate the loop, and if the smaller array is out of range then add a 0 for example or anything. You know what i mean? Thanks for your time and effort! – christian890 Aug 04 '17 at 11:06
  • Basicly like i posted in the main Post the update. I search for the biggest ampount of Col and Rows and then iterate through it but the check if field exists in array a / B doesnt work...i need a way to check if field exists and if not fill it with 0. Try / catch does work just fine BUT it takes ages... Since i have arround 10k fields and that multiple times... so i need a faster solution – christian890 Aug 04 '17 at 11:12
  • 1
    @christian890, seed edited answer, you can perform some operation if arrays have enough elements (inside `if`) or do another operation when there is no elements in some array (inside `else`) – Roman Aug 04 '17 at 11:16
  • Ohh yeah damn you are really awesome! this fixes some more problems! I have 1 huge problem left though: what if array1[1].length != array1[0].length (this could be for array 1 or array 2 and there for each field =/) i would still get the out of range then =/ THANKS! i really appreciate it EDIT: is the array.length possible for variable rows/columns? and if so how? that would solve it i think Or does the 2nd part with b < array1[a] do exactly this? – christian890 Aug 04 '17 at 11:22
  • 1
    @christian890, see edit - changed nested `for` loop - `for (int b = 0; b < Math.Max(array1[a].Length, array2[a].Length); b++) ` – Roman Aug 04 '17 at 11:25
  • Ill try this asap! Seems to be a real nice solution! ill answer again in some mins! thanks!!!! – christian890 Aug 04 '17 at 11:28
  • Thank you so much! its working like a charm! now i just came upon some errors for example if 1 array didnt have any values at all -> out of range but i could fix them easily with this code! THANK you so much!!! Have a great weekend! – christian890 Aug 04 '17 at 12:06
  • May i ask one more thing: When one of the arrays has 0 values in it i still have a problem i cant solve atm. i tried checking it with " if (Array1[a].Length != 0) but this throws the outofrange error when the length is 0 since it cant access array1[a] any idea how to fix this? – christian890 Aug 04 '17 at 12:13
  • 1
    @christian890, Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/151060/discussion-between-roma-doskoch-and-christian890). – Roman Aug 04 '17 at 12:15
  • 1
    @christian890 Please read : [about follow-up questions](https://meta.stackoverflow.com/q/266767/327083) – J... Aug 04 '17 at 12:24
  • Thank you ROma! I didnt know such chat exists! you really helped me out big time! – christian890 Aug 06 '17 at 19:25