[英]print Section name and it's own employees with their ages using jagged arrays
I am trying to use jagged arrays in order to print sections' names with their names of employees and ages I tried like this:我正在尝试使用锯齿状的 arrays 来打印部门名称及其员工姓名和年龄我试过这样:
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?我的问题是迭代员工 arrays 来打印它们,我该怎么做? and if there were examples it will be more better
如果有例子会更好
You have a 2-D Array structure for employeeTree.您有一个用于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:这将打印以下 output:
mark标记
20 20
mike麦克风
30 30
michel米歇尔
3 3
joerge豪尔赫
40 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]}");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.