我目前正在研究一个程序来实现Dijkstra的算法只是为了好玩。它可能远远超出我的技能水平,但我想我会放弃它。 到目前为止,我已经创建了一个顶点类,其中包含一个名为Connections的属性,它是与其连接的其他顶点的列表。当我使用我的表单将一个新的顶点添加到我的图形(顶点列表)时,它似乎不会记住我分配的连接。我已经尝试过调试,并且在它被分配到课堂后似乎神秘地消失了。任何帮助将不胜感激。列表神秘地删除自己似乎没有解释
Form1.vb的
Public Class Form1
Dim Graph As New List(Of Vertex)
Dim vertS As New Vertex("S", New List(Of Vertex)(), New List(Of Double)(), True, 0)
Dim vertT As New Vertex("T", New List(Of Vertex)(), New List(Of Double)(), False)
Dim tempLengths As New List(Of Double)
Dim tempConnections As New List(Of Vertex)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Graph.Add(vertT)
Graph.Add(vertS)
ListBox1.Items.Clear()
ListBox2.Items.Clear()
For Each vert In Graph
ListBox1.Items.Add(vert.Key)
ListBox2.Items.Add(vert.Key)
Next
End Sub
Sub RefreshAll()
TextBox1.Text = ""
tempConnections.Clear()
tempLengths.Clear()
ListBox1.Items.Clear()
ListBox2.Items.Clear()
ListBox3.Items.Clear()
For Each vert In Graph
ListBox1.Items.Add(vert.Key)
ListBox2.Items.Add(vert.Key)
Next
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
Dim chosenVert = Graph(ListBox1.SelectedIndex)
TextBox1.Text = chosenVert.Key
TextBox2.Text = ""
ListBox3.Items.Clear()
For Each con In chosenVert.Connections
ListBox3.Items.Add(con.Key)
Next
End Sub
Private Sub btnAddCon_Click(sender As Object, e As EventArgs) Handles btnAddCon.Click
ListBox3.Items.Add(ListBox2.SelectedItem)
tempConnections.Add(Graph(ListBox2.SelectedIndex))
tempLengths.Add(Val(TextBox2.Text))
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Graph.Add(New Vertex(TextBox1.Text, tempConnections, tempLengths, False))
RefreshAll()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Graph(ListBox1.SelectedIndex).Connections.AddRange(tempConnections)
Graph(ListBox1.SelectedIndex).Lengths.AddRange(tempLengths)
Graph(ListBox1.SelectedIndex).Key = TextBox1.Text
RefreshAll()
End Sub
End Class
Vertex.vb
Public Class Vertex
Private pCompValue As Double
Private pCompleted As Boolean
Private pConnections As New List(Of Vertex)
Private pLengths As New List(Of Double)
Private pKey As String
Property CompValue() As Double
Get
Return pCompValue
End Get
Set(value As Double)
pCompValue = value
End Set
End Property
Property Completed As Boolean
Get
Return pCompleted
End Get
Set(value As Boolean)
pCompleted = value
End Set
End Property
Property Connections As List(Of Vertex)
Get
Return pConnections
End Get
Set(value As List(Of Vertex))
pConnections = value
End Set
End Property
Property Lengths As List(Of Double)
Get
Return pLengths
End Get
Set(value As List(Of Double))
pLengths = value
End Set
End Property
Property Key As String
Get
Return pKey
End Get
Set(value As String)
pKey = value
End Set
End Property
Sub New(ByVal pKey As String, ByVal pConnections As List(Of Vertex), ByVal pLengths As List(Of Double), ByVal pCompleted As Boolean, Optional ByVal pCompValue As Double = 10000000000)
Key = pKey
Connections = pConnections
Lengths = pLengths
Completed = pCompleted
CompValue = pCompValue
End Sub
End Class
我创建Button1.click一个新的顶点应该被添加到 “图” 列表中。顶点加入,但没有任何连接,当我试图重新填充ListBox3
学习在此处粘贴和格式化代码,并且不要将链接发布到代码(外部网站可能不可用...) – crashmstr 2014-09-29 19:18:08
我已经添加了您的pastebin中的代码 - 请查看您的源代码通过点击*编辑*来查看如何添加格式化的代码。 – 2014-09-29 19:20:55
非常感谢那些为我编辑过的人。 – 2014-09-29 19:21:12