Iterate through 2 dimensional array c#

后端 未结 7 1508
悲&欢浪女
悲&欢浪女 2020-12-03 16:37
for(int k=0;k <= odds.GetLength(-1);k++)

The above line of code is supposed to iterate through a two dimensional array of type Double but keeps

相关标签:
7条回答
  • 2020-12-03 17:14

    I see one or two problems depending on how you intend to use it:

    First off, GetLength(ind dimension) returns length of specified dimension, starting from 0. In case of a two-dimensional array, correct indices would be 0 and 1.

    The second problem is that you are doing <= instead of < for loop condition, which might also go out of bounds since last index is length-1 and not length.

    StriplingWarrior and Gilad Naaman posted code examples so I'll skip that.

    0 讨论(0)
  • 2020-12-03 17:20

    If odds is a two-dimensional array, then its dimensions will be called 0 and 1. Trying to access dimension -1 will yield an IndexOutOfRangeException.

    0 讨论(0)
  • 2020-12-03 17:22

    Well, usualy when you want to iterate on a 2D array:

    for(int col = 0; col < arr.GetLength(0); col++)
        for(int i = row; row < arr.GetLength(1); row++)
            arr[col,row] =  /*something*/;
    

    Arrays are always zero-based, so there's no point of trying to get something at -1 index.

    0 讨论(0)
  • I followed the accepted answer but it throw exception in my code.

    This is my way to iterate 2 dimension array using 2 foreach loop. I share for whom concerned.

    int i = 0;
          
    foreach (var innercell in cells)
    {
        int j = 0;
        foreach (var item in innercell)
        {
           if (j == cell.Col && cell.Row == i)
           {
             item.TagName = cell.TagName;
             item.Text = cell.Text;
             exist = true;
             break;
            }
            j++;
         }
         i++;
    }
    
    0 讨论(0)
  • 2020-12-03 17:30

    You are passing an invalid index to GetLength. The dimensions of a multidimensional array are 0 based, so -1 is invalid and using a negative number (or a number that is larger than the number of dimensions - 1) would cause an IndexOutOfRangeException.

    This will loop over the first dimension:

    for (int k = 0; k < odds.GetLength(0); k++)
    

    You need to add another loop to go through the second dimension:

    for (int k = 0; k < odds.GetLength(0); k++)
        for (int l = 0; l < odds.GetLength(1); l++)
            var val = odds[k, l];
    
    0 讨论(0)
  • 2020-12-03 17:34
    string[,] arr = new string[2, 3];
            arr[0, 0] = "0,0";
            arr[0, 1] = "0,1";
            arr[0, 2] = "0,2";
    
            arr[1, 0] = "1,0";
            arr[1, 1] = "1,1";
            arr[1, 2] = "1,2";
    
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr.GetLength(1); j++)
                {
                    Response.Write(string.Format("{0}\t", arr[i, j]));
                }
                Response.Write("<br/>");
            }
    
    0 讨论(0)
提交回复
热议问题