.NET中的多深度数组

时间:2014-04-24 12:39:46

标签: arrays vb.net

我一直试图在最后一小时内完成这项任务,但仍然可以让它发挥作用 我正在尝试创建一个多深度数组。让我解释一下myslef。

Boss
    Manager
        Employee
        Employee
        Employee
    Manager
        Employee
        Employee
        Employee
Boss
    Manager
        Employee
        Employee
        Employee
    Manager
        Employee
        Employee
        Employee
Boss
    Manager
        Employee
        Employee
        Employee
    Manager
        Employee
        Employee
        Employee

他们必须有一个标签,这里是Boss,Manager或Employee的名字。

有没有办法做到这一点?

由于

2 个答案:

答案 0 :(得分:0)

可能更好的方法是让一个类代表一个人的层次结构,并在其下面列出人员。就个人而言,multidepth数组可能有点复杂,特别是对于要维护代码的人。

Sub Main()

    Dim bosses As New List(Of Hierarchy)

    bosses.Add(New Hierarchy)
    bosses(0).Person = New Person
    bosses(0).Person.Name = "Boss"

    bosses(0).Childs.Add(New Hierarchy)
    bosses(0).Childs(0).Person = New Person
    bosses(0).Childs(0).Person.Name = "Manager"

    bosses(0).Childs(0).Childs.Add(New Hierarchy)
    bosses(0).Childs(0).Childs(0).Person = New Person
    bosses(0).Childs(0).Childs(0).Person.Name = "Employee"

    bosses(0).Childs(0).Childs.Add(New Hierarchy)
    bosses(0).Childs(0).Childs(1).Person = New Person
    bosses(0).Childs(0).Childs(1).Person.Name = "Employee"

    For Each boss In bosses
        Console.WriteLine(boss.Person.Name)

        For Each manager In boss.Childs
            Console.WriteLine("   " & manager.Person.Name)

            For Each employee In manager.Childs
                Console.WriteLine("      " & employee.Person.Name)
            Next
        Next
    Next

End Sub

Class Hierarchy
    Public Person As Person
    Public Childs As New List(Of Hierarchy)
End Class

Class Person
    Public Name As String
End Class

答案 1 :(得分:0)

以下是我设法做到这一点的方法:

Public Class sousdir
    Public name As String
    Public services As New List(Of String)
End Class

Public Class direction
    Public name As String
    Public sousdirs As New List(Of sousdir)
End Class

Dim directions As New List(Of direction)

directions.Add(New direction)
directions(0).name = "Direction 1"
directions(0).sousdirs.Add(New sousdir)
directions(0).sousdirs(0).name = "Sous Direction 1"
directions(0).sousdirs(0).services.Add("Service 1")
directions(0).sousdirs(0).services.Add("Service 2")
directions(0).sousdirs(0).services.Add("Service 3")
directions(0).sousdirs.Add(New sousdir)
directions(0).sousdirs(1).name = "Sous Direction 2"
directions(0).sousdirs(1).services.Add("Service 1")
directions(0).sousdirs(1).services.Add("Service 2")
...

结果格式化 Result

这将如何帮助一些人