使用锯齿状数组打印部门名称及其年龄的员工

print Section name and it's own employees with their ages using jagged 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" }};

我的问题是迭代员工数组以打印它们, 我该怎么做?如果有例子就更好了

        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]}");
            }
        }

您有一个 employeeTree 的二维数组结构。 因此,直接的方法是在二维数组上迭代两次以访问元素。

最简单的方法如下:

foreach(var emp in employeeTree.Where(x => x != null) )
                foreach(var object1 in emp)
                    Console.WriteLine(object1.ToString());

这将打印以下输出:

标记

20

麦克

30

米歇尔

3

乔格

40

您可以对其进行格式化,以便将它们打印在一行中,如下所示:

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]);
                }