I am trying to use jagged arrays in order to print sections' names with their names of employees and ages I tried like this:
string[] sections= new string[50];
sections[0] = "It";
sections[1] = "Hr";
string[][,] employeeTree = new string[6][,];
employeeTree [0] = new string[,] { {"mark","20"},{ "mike", "30" },{ "michel", "3" },{ "joerge", "40" }};
My problem is iterating the employees arrays to print them , how can I do it? and if there were examples it will be more better
You have a 2-D Array structure for employeeTree. So the straightforward way would be to iterate twice on the 2-D array to access the elements.
The simplest way is like below:
foreach(var emp in employeeTree.Where(x => x != null) )
foreach(var object1 in emp)
Console.WriteLine(object1.ToString());
This will print the below output:
mark
20
mike
30
michel
3
joerge
40
You can do formatting on this to print them in a single line like below:
foreach (var emp in employeeTree.Where(x => x != null))
for (int i = 0; i < emp.GetLength(0); i++)
{
Console.WriteLine(emp[i,0] + emp[i,1]);
}
foreach(string[,] employee in employeeTree.Where(x => x != null)) {
int max = employee.GetLength(0);
for (int i = 0; i < max; i++) {
Console.WriteLine($"{employee[i, 0]}, {employee[i, 1]}");
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.