Explicite defined array
Dim MyVariable As DateType = x
Dim strName As String = "Rach"
Dim MyArray(5) As DataType
Dim strNames(2) As String
' Assign values
strNames(0) = "Rach"
strNames(1) = "Pam"
strNames(2) = "David"
An array contains multiple elements - array index begins at 0
Implicitly sized Array
Declare an array and populate with no definate value assigned
Dim strNames() As String = ("Baker", "Lopez", "Buck", "Chan", "Tirrell")
Dim intReservations() As Integer = {4, 5, 12, 2, 8}
Parallel Arrays
Dim intDinnerBookings(2) As Integer
Dim strNames(2) As String
' Loop to populate
' Data Table - strNames
' Bookings - intDinnerBookings
Dim ItemArray() As String = {"x", "y", "z"}
' 3 elements
Dim intCount As Integer
'Declared an Integer Variable
For intCount = 0 To ItemArray.Length -1
'Length = number of elements so .Length = -1
'Extract each item into the loop - start at 0
lstBox.Item.Add(ItemArray(intCount))
Next
Length returns the number of elements - not the upperbound
Index begins at zero
if the -1 is omitted from the .Length - it will loop 4 times instead of 3
= IndexOutOfRange Exception. A Try-Catch statement can catch this exception, but it is best to stay within the array boundaries of zero and the upper-bound array subscript.
Use loops to populate the array
Sorting Arrays
When applied - (default) the lowest value is placed in the 1st element in the array with an index of zero, next lowest is placed in the second element, etc
Array.Sort(ArrayName)
'Sort is a Function
Dim intAges() as Integer = {16, 64, 41, 8, 19, 81, 23}
Array.Sort(intAges)
Search Arrays
Sequential - searching each element with first name - not particularly efficient in large list
Binary - searches a sorted array for a value using a binary search algorithm = by repeatedly dividing the search interval in half = sort first - splits in half - searches on upper half of array to match the index - if match continues to split - no match - searches on lower half - them splits until found
Sunday, August 9, 2009
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment