|
|
 |
|
|
|
|
Visual Basic 2008 9.0 .NET Examples and Ebook
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Array Datatype
| An array variable contains Nothing or a reference to an array instance.
When an identifier of an array variable is used as an expression ( for instance expression array1 in line (1) ) this expression will evaluate to the reference of that array instance. Line (1) will assign the reference of the first array to the array variable array2. Both variables ( array1 and array2 ) now point to the same array instance. As you can see lines (2) and (3) have the same result.
Lines (4) and (5) would result in a compile error, because variables of types Integer(,) ( two-dimensional Integer array ) or Short() ( one-dimensional Short array ) cant hold a reference to an instance of an array of type Integer() ( one-dimensional Integer array ).
Only array variables of type Integer() ( one-dimensional Integer array ) can hold a reference to an one-dimensional Integer array instance. Well actually this is not completely correct, array variables of type Array ( System.Array ) can hold references to any type of arrays. The reason for this is that 'Array' is the base type for all other arraydatatypes. All variables declared of a base type can hold references to instances of derived types. In the topics about inheritance and polymorphism we'll discuss this in detail. So variable array6 can for instance hold a reference to a two-dimensional Byte array (6) or for instance a reference to a one-dimensional Integer array (7). |
| Module Example1
Sub Main()
Dim array1() As Integer = {1, 2, 3, 4, 5}
Dim array2() As Integer = array1
Console.WriteLine(array1(3))
Console.WriteLine(array2(2))
array1(3) = 33
Console.WriteLine(array1(3))
Console.WriteLine(array2(3))
array2(4) = 44
Console.WriteLine(array1(4))
Console.WriteLine(array2(4))
Dim array5(,) As Byte = {{1, 2}, {3, 4}}
Dim array6 As Array = array5
Console.WriteLine(array6(1, 1))
array6 = array2
Console.WriteLine(array6(4))
Console.ReadLine()
End Sub
End Module Download Broncode |
| Output : 4
3
33
33
44
44
4
44 |
Up
Jagged Arrays
| It is possible to create an array of which the elements are references to other arrays. This is for instance useful when a collection of collections needs to be managed.
Next example declares a array cities with two elements of type Array. These elements can now hold a reference to any type of array. cities(0) holds a reference to a one-dimensional String array (1). cities(1) holds a reference to a one-dimensional Integer array (2). |
| Module Example2
Sub Main()
Dim names() As String = {"Brussels", "Antwerp", "Ghent"}
Dim zipCodes() As Integer = {1000, 2000, 0}
Dim cities(1) As Array
cities(0) = names
cities(1) = zipCodes
Console.WriteLine(names(1))
Console.WriteLine(cities(0)(1))
cities(1)(2) = 9000
Console.WriteLine(zipCodes(2))
Console.ReadLine()
End Sub
End Module Download Broncode |
| Output : Antwerp
Antwerp
9000 |
| As you can see on lines (3) and (4) expressions names and cities(0) both evaluate to the reference of the array instance containing the names of the cities. So expressions names(1) and cities(0)(1) both address the second element of the same array. |
Up
Exercise
| Task :
Make a program containing 3 arrays : - a one-dimensional String array containing names of students - a one-dimensional Integer array containing scores of students - a jagged array, with 2 elements, pointing to the previous two arrays
Try for exercise only to use the jagged array to address the names and scores of the students, so avoid using the first 2 arrays.
Make it possible to add students and their scores. The program calculates and stores the averages score for each student. Store this average score in an extra row or column of the scores-array. |
| Output : Student Count ? <i>2</i>
Topic Count ? <i>2</i>
Name Student 1 ? <i>John</i>
John Score Topic 1 ? <i>4</i>
John Score Topic 2 ? <i>6</i>
Name Student 2 ? <i>Paul</i>
Paul Score Topic 1 ? <i>6</i>
Paul Score Topic 2 ? <i>8</i>
Name : John
Score Topic 1 : 4
Score Topic 2 : 6
Average Score : 5
Name : Paul
Score Topic 1 : 6
Score Topic 2 : 8
Average Score : 7 |
| Module ExerciseSolution
Sub Main()
Console.Write("Student Count ? ")
Dim studentCount As Integer = Console.ReadLine()
Console.Write("Topic Count ? ")
Dim topicCount As Integer = Console.ReadLine()
Console.WriteLine()
Dim studentUpperbound As Integer = studentCount - 1
Dim names(studentUpperbound) As String
Dim topicUpperbound As Integer = topicCount
Dim scores(studentUpperbound, topicUpperbound) As Double
Dim students(1) As Array
Const namesIndex As Integer = 0, scoresIndex As Integer = 1
students(namesIndex) = names
students(scoresIndex) = scores
Dim studentIndex, topicIndex As Integer
For studentIndex = 0 To studentUpperbound
Console.Write("Name Student " & (studentIndex + 1) & " ? ")
students(namesIndex)(studentIndex) = Console.ReadLine()
For topicIndex = 0 To topicUpperbound - 1
Console.Write(students(namesIndex)(studentIndex) & _
" Score Topic " & (topicIndex + 1) & " ? ")
students(scoresIndex)(studentIndex, topicIndex) = _
Console.ReadLine()
Next
Console.WriteLine()
Next
For studentIndex = 0 To studentUpperbound
For topicIndex = 0 To topicUpperbound - 1
students(scoresIndex)(studentIndex, topicUpperbound) += _
students(scoresIndex)(studentIndex, topicIndex)
Next
students(scoresIndex)(studentIndex, topicUpperbound) /= _
topicUpperbound
Next
Console.WriteLine()
For studentIndex = 0 To studentUpperbound
Console.WriteLine("Name : " & students(namesIndex)(studentIndex))
For topicIndex = 0 To topicUpperbound - 1
Console.WriteLine("Score Topic " & (topicIndex + 1) & " : " & _
students(scoresIndex)(studentIndex, topicIndex))
Next
Console.WriteLine("Average Score : " & _
students(scoresIndex)(studentIndex, topicUpperbound))
Console.WriteLine()
Next
Console.ReadLine()
End Sub
End Module Download Broncode |
This version ( published on 2008-06-24 ) is printed from http://www.studyvb.com, visit the website for more recent information.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|