2014-09-29 34 views
-2

我目前正在研究一个程序来实现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

+2

学习在此处粘贴和格式化代码,并且不要将链接发布到代码(外部网站可能不可用...) – crashmstr 2014-09-29 19:18:08

+0

我已经添加了您的pastebin中的代码 - 请查看您的源代码通过点击*编辑*来查看如何添加格式化的代码。 – 2014-09-29 19:20:55

+0

非常感谢那些为我编辑过的人。 – 2014-09-29 19:21:12

回答

0

你似乎有这样一种印象,这种线下:

Graph.Add(New Vertex(TextBox1.Text, tempConnections, tempLengths, False)) 

是要复制分配给tempConnectionsList(Of Vertex)对象。不是这样。只有一个List(Of Vertex)所以当你以后做:

tempConnections.Clear() 

你清除有且只有List(Of Vertex)您的新Vertex简称。

想想这个。假设你和我将要分享一间公寓。你已经住在那里,并用刀和叉填满了餐具。当我搬进去的时候,那个餐具画也是我的餐具画,也是你的。这是否意味着魔术现在有两个餐具抽签?不,当然不。只有一个我们都使用的餐具画。现在,如果我清理了我的餐具,您打算如何打开餐具画画,您会看到什么?你的餐具画是空的,对吗?

这是完全一样的情况。出于某种原因称为面向对象的编程。编程对象应该像真实生活的对象一样运行,并且它们在您的程序中。只有一个List(Of Vertex),你指的是来自多个地方。如果你在其中一个地方清除它,那么其他地方会看到一个空的清单。