1

次の再帰関係をサポートする多次元配列を作成しようとしています (DATA はデータベース テーブルから取得されます)。

この多次元配列は、以下のリストを生成するために使用されます。VB.NET での多次元配列の経験はほとんどありません。どんな助けでも大歓迎です。これを達成するためのより良い方法があると思われる場合は、お知らせください。

データ

ID      NAME                PARENTID
10      Bobby Brown         50          
20      Dave Matthew        80
30      Sergey Boostad      50
40      Linda View          50
50      Bill Lumberg        
60      Rina Gina           50
70      Ben Thompson        100
80      Maria Tree          50
90      Gustav Duffield     80
100     Jon Theodore        
110     Cedric Loomis       100 
120     Jeremy Oscar        100

OUTPUT(達成する)

[50] - Bill Lumberg
    [10] - Bobby Brown
    [30] - Sergey Boostad
    [40] - Linda View
    [60] - Rina Gina
    [80] - Maria Tree
        [20] - Dave Matthew
        [90] - Gustav Duffield
[100] - Jon Theodore    
    [70] - Ben Thompson
    [110] - Cedric Loomis       
    [120] - Jeremy Oscar
4

1 に答える 1

2

ツリーをメモリに保存するには、次のようなクラスを作成します。

Public Class NameNode
    Public Sub New(name As String)
        Me.Name = name
    End Sub

    Public Property Name As String
    Public Level As Integer
    Public Property Children As New List(Of NameNode)
End Class

次に、次のように使用できます。

Dim bill As New NameNode("Bill Lumberg")
bill.Children.Add(New NameNode("Bobby Brown")
bill.Children.Add(New NameNode("Sergey Boostad")

フラットからそれを埋めるにはDataSet、たとえば次のような再帰的なメソッドを作成する必要があります。

Public Function BuildNode(data As DataSet, nameId As Integer, level As Integer), As NameNode
    Dim node As New NameNode()
    node.Level = level
    ' Find name with the ID in the dataset and set the node's name property accordingly
    Dim childIds As New List(Of Integer)
    ' Search Get a list of all the name ID's that have the current ID as their parent
    For Each i As Integer In childIds
        node.Children.Add(BuildNode(data, i, level + 1))
    Next
    Return node
End Function

次に、次のように呼び出すことで、BillLumbergブランチ全体を構築できます。

Dim bill As NameNode = BuildNode(data, 50, 0) 
于 2013-02-20T15:57:00.677 に答える