1

So, I have a list which has an array of strings inside it, which (the arrays) are of different lengths.

foodmenu.menumethod();
var menugrid = foodmenu.menumethod();

// method for category lists

List<string> ingcategory = new List<string>();

for (int i = 0; i < menugrid.Count; i++)
{  
     ingcategory.Add(menugrid[i][]);       
}

return ingcategory;

I want to be able to access all the elements in the string array (which are the arrays inside the list) using a loop but a number of elements the string array contains are not the same for each item in the list.

what the current menugrid variable contains:

1 a b c d e f 
2 a b 
3 a b c d e f g h i j
4 a b c d e f h
5 a b c d
6 a b 
7 a 

currently, it only goes to one to seven and I have to manually place the second coordinate in the place of Y due to me getting out of bound errors.

Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
xxish
  • 49
  • 9

2 Answers2

0

use a foreach loop to access each string[] within the menugrid, that way you don't have to concern yourself with indexers.

foreach (string[] array in menugrid)
{
    // do something with the array           
}

or if you want to go another level deeper:

foreach (string[] array in menugrid)
{
    // do something with the array if necessary
    foreach (string item in array){
       // do something with the items within the array
    }            
}
Ousmane D.
  • 54,915
  • 8
  • 91
  • 126
0

Here's how you can traverse through your list of array of strings:

foreach(var menu in menugrid)
{
    foreach(var item in menu)
    {
        // TODO: Do something with each item
        Console.Write(item);
    }
    Console.WriteLine("");
}
degant
  • 4,861
  • 1
  • 17
  • 29