梓囚徒貧圭�鮗� ○ 賜 ★ 辛酔堀貧和鍬匈��梓囚徒貧議 Enter 囚辛指欺云慕朕村匈��梓囚徒貧圭�鮗� ● 辛指欺云匈競何��
!!!!隆堋響頼��紗秘慕禰厮宴和肝写偬堋響��
End Structure
In this declaration�察�Connections is an array of strings that references other city names and
is what humans would see when they look at a table showing all of the connections for a partic
ular city。 The problem with using strings is that it is inefficient from a puting perspective。
To traverse a tree of cities�察�you would first traverse the city names�察�resolve the city name to a
Node object�察�and then traverse the node。 The string array approach requires an extra�察�unneces
sary step。 So the more efficient and programmatic approach is to have an array of Node instances。
By using the declaration where Connections is an array of Node instances�察�you have both
the city name and available connections in one cohesive object。
Instantiating and Initializing a Node
In previous code�察�you have seen how objects can be instantiated using the New keyword。 To
instantiate a type�察�you always use the New keyword。 After the New keyword is the type that you
want to instantiate�察�followed by a set of parentheses。 A node is instantiated using the following
code�此�
Dim city As Node = New Node�┌�
If you look only at the identifier Node with parentheses�察�you would get the impression that
you are calling a method that has no parameters。 The impression is correct�察�but it is a special
type of method call�察�and that is made apparent by the use of the New keyword。 The method that
is being called is known as a constructor。 Every type has a constructor�察�and it can be used to
initialize the state of the object before being returned to the caller。
In the declaration of Node�察�there is no defined constructor�察�and thus a default constructor
is provided by the CLR。 The default constructor does nothing and has no parameters。
After having instantiated a node�察�we can assign the data members�察�as in the following
code。
city。CityName = ;Montreal;
city。X = 0。0
city。Y = 0。0
Assigning the data members results in setting the city name to Montreal and the coordi
nates to ��0��0��。
This is all fine�察�but shouldn¨t we need to provide some data members when creating a city
node�拭�Does it make sense to instantiate a node without defining the name and coordinates of
the city�拭�Technically�察�a node does not need to be assigned�察�but logically speaking�察�an unassigned
node is quite useless。 And remember that we are working on defining an intelligent data struc
ture�察�thus a Node instance without city name and coordinates is logically not a valid Node。 We
also need to have a root for the Node structure�察�which we will set up as an array。
You can enforce a verifiable correct initial state by defining a constructor with parameters�察 �
rather than using the default constructor�察�as in the following example。 When your code provides
´´´´´´´´´´´´´´´´´´´´´´Page 113´´´´´´´´´´´´´´´´´´´´´´´
CH AP T E R 4 * L E A R N I N G A B OU T D AT A S TR U CT U R E S�察 �DE CI SI ON S�察 �A N D L O OP S 91
a constructor�察�regardless of the declaration�察�the default constructor is not generated and is not
accessible。
Public Structure Node
Public Shared RootNodes As Node�┌�
Public CityName As String
Public X As Double
Public Y As Double
Public Connections As Node�┌�
Public Sub New��ByVal city As String�察�ByVal X As Double�察�ByVal Y As Double��
Me。CityName = city
Me。X = x
Me。Y = y
Me。Connections = Nothing
End Sub
End Structure
*Note Nothing is a special value that you can assign to reference variables。 It means that the reference
variable does not point to a real object。 Nothing is a useful value to assign to reference variables in a constructor�察 �
if you don¨t have a meaningful object that they can point to at the outset。
To define a constructor�察�you define a method of type Sub called New�┌�。 And�察�in most cases�察 �
you will use public scope。 The parameters of the constructor represent the three pieces of
information that are required to instantiate a valid state。 Within the constructor�察�the data
members are assigned the values of the parameters。
The defined constructor has parameters�察�which means that to instantiate Node�察�you need
to provide the three pieces of data。 Thus�察�to instantiate Node�察�you need to provide enough data
to make the node logical。 The original instantiation code would not pile�察�so to pile the
code�察�you need to modify the instantiation to the following�此�
Dim city as Node = New Node�─�Montreal;�察�0。0�察�0。0��
The declaration of the node might reference incorrect data�察�but that is not the responsibility
of the intelligent data structure。 An analogy is that a word processor by itself is not responsible for
making sure that the text you write makes sense。 The role of the word processor is to give you
the ability to construct intelligent text。
Examining the Problem of Referencing Using Value Types
As you¨ve learned�察�a value type is stored on the stack�察�and its contents are copied�察�not refer
enced。 When you are trying to build a tree structure with a value type�察�references that were
assigned are not updated with the correct information because values are copied。 This effect
can be demonstrated by going through a longer example of building a data structure of cities
that can be reached from another city。 To start off�察�consider the following declaration of all the
cities and their coordinates。
´´´´´´´´´´´´´´´´´´´´´´Page 114´´´´´´´´´´´´´´´´´´´´´´´
92 CH AP T E R 4 * L E A R N IN G AB OU T D AT A S TR U CT U R E S�察 �DE CI SI ON S�察 �A N D L O OP S
Dim montreal As Node = New Node�─�Montreal;�察�0�察�0��
Dim newyork As Node = New Node�─�New York;�察�0�察 �3��
Dim miami As Node = New Node�─�Miami;�察 �1�察 �11��
Dim toronto As Node = New Node�─�Toronto;�察 �4�察 �1��
Dim houston As Node = New Node�─�Houston;�察 �10�察 �9��
Dim losangeles As Node = New Node�─�Los Angeles;�察 �17�察 �6��
Dim seattle As Node = New Node�─�Seattle;�察 �16�察 �1��
This code creates several Node objects that represent all of the cities from Figure 4´7。 The
individual objects are cities without connections�察�and the next step is to connect one city to
another。 We need to allocate and assign the Connections data member。
Having initialized all of the individual cities�察�the next step is to cross´reference the destina
tions of each city。 In Visual Basic�察�the array requires some special understanding。 The Connections
data member is an empty array without any elements。 This is not a problem�察�as long as you
don¨t attempt to reference or assign any of the elements。
You need to allocate space for the array so that you can store the individual cities。 One
solution is to reassign the array to another array that contains elements�察�as follows�此�
montreal。Connections = New Node�┌� ��newyork�察�toronto�察�losangeles��
In this example�察�the Connections data member is reassigned with the contents of another
array that has been allocated to contain the three cities�察�newyork�察�toronto�察�and losangeles。
Think of the array as a basket。 Using this approach�察�the original empty basket has been replaced
with a basket of three items。 These three items can be referenced using three indices ��0 = newyork�察 �