Visual Basic Ch. 8-9
what does subscript numbering begin with
0 or zero based
How are arrays and collections different
An array is a feature of the Visual Basic language that inherits the Array class. Collections are other classes in the .NET Framework. Collection classes provide methods to perform operations that arrays don't provide.
Example that displays the list
Dim salesTotalsStrign as string = "" For each d as decimal in salesTotals salesTotalsString &= d.tostring & vbCrLf Next messagebox.show(salesTotalsString, "Sales Totals")
Example of a list of string elements
Dim titles as new list (of string)
Example that checks for an element in the list and removes it if it exists
Dim x as Decimal = 2745.73D If salesTotals.Contains(x) then salesTotals.remove(x) End if
What is used to right align data
PadLeft(totalwidth)
What is used to left align data
PadRight(totalwidth)
What function will convert Char to Integer
ASCII("a") equals 97
What adds an element to the end of a list and returns the element's index
Add(object)
What retrieves the next item in the queue without deleting it
Peek() - method of Queue
What converts the DateTime value to a string that includes the numbers for the month, day and year
ToShortDateString() - - DateTime methods
What converts the DateTime value to a string that includes the hours and minutes
ToShortTimeString() - - DateTime methods
What results in a string with lowercase letters converted to uppercase called
ToUpper
What returns the current date
Today- DateTime property
What removes leading and trailing spaces
Trim
What removes trailing spaces
TrimEnd
What removes leading spaces
TrimStart
How are arrays and collections similar
both can store multiple elements, which can be value types or reference types
Example of processing array contents with multiplication
decGrossPay= intHours(3) * decPayRate
what do you use to compute the average or you will lose any decimal places
floating-point division
Example of searching for arrays
found = False subscript = 0 Do While found is False and subscript < array's length If array(subscript) = searchValue Then found = True position = subscript End If subscript += 1 End While
Example of split method
fullName = fullName.Trim Dim names() As String = fullName.Split(CChar(" ")) Dim firstName As String = names(0)
What results in a string with uppercase letters converted to lowercase
ToLower
Example that retrieves the first value from the list
Dim sales1 As Decimal = salesTotals(0)
What searches a one dimensional array that's in ascending order for an element with a specified value and returns the index for that element
BinarySearch(array,value)
What searches a list for a specified object and returns the index for that object
BinarySearch(object)
What allows an array to be assigned to another array
ByRef
What prevents an array from being assigned to another array
ByVal
What function with convert Integer to Char
CHR(97) equals "a"
What gets or sets the number of elements the list can hold
Capacity
What removes all elements from the list and sets its Count property to zero
Clear()
Example of a constant
Const intMAX_SUBSCRIPT As integer = 100 Dim intArray(intMAX_SUBSCRIPT) as integer
Example of using array elements to store input
Const intMAX_SUBSCRIPT As integer = 9 Dim intSeries(intMAX_SUBSCRIPT) As Integer Dim intCount as integer For intCount = 0 to intMAX_SUBSCRIPT intSeries(intCount) = CInt (InputBox("Enter a number.")) Next
What returns a Boolean value that indicates if the list contains the specified object
Contains(object)
What copies some or all of the values in one array to another array
Copy(array1, array2, length)
What gets the number of elements in a list
Count
What gets the object at the front of the queue and removes it from the queue
Dequeue()
Example that starts at the beginning of the string and continues until it reaches the 7th character of the string
Dim FirstName as string Dim FullName as string = "George Washington" FirstName = FullName.Substring (0.6)
Example of starting at the 8th character in the string and continues to the end of the string
Dim LastName as string Dim FullName as string = "George Washington" LastName = FullName.Substring(7)
Example of array
Dim Sales(6) As Decimal ... For I as Integer = 0 to 6 WeeklySales += Sales(I) Next
Example of determining the length of a string
Dim ZipCode As String Do ZipCode = InputBox("Enter 5 digit zip code") If ZipCode.Length <> 5 Then MsgBox("Wrong size - Five digits only",, "Zip Code") End If Loop Until ZipCode.Length = 5
Example of replace method
Dim birthDate As String = "12-27-2012" birthDate = birthDate.Replace("-", "/")
Example of uses an index to access a characters in a string
Dim chars As String = "abcdefg" Dim a As Char = chars(0) Dim b As Char = chars(1)
Example of uses a for loop to access each character in the string
Dim charsAndSpaces As String = "" For i As Integer = 0 To chars.Length - 1 charsAndSpaces &= chars(i) & " " Next MessageBox.Show(charsAndSpaces, "String Test")
Example of declaring a two dimensional array
Dim dblScores(2,3) as double
Example of copying one array's contents to another
Dim inches() As Double = {1, 2, 3, 4} Dim centimeters(3) As Double Array.Copy(inches, centimeters, inches.Length) For i As Integer = 0 To centimeters.Length - 1 centimeters(i) *= 2.54 Next
Example of for each loop
Dim intArray () As Integer = {10, 20, 30, 40, 50, 60} For Each intVal As Integer In intArray lstShow.Items.Add(intVal) Next
Example of array declaration
Dim intHours (6) as integer (which means that the array's highest subscript is 6)
Example of array initialization
Dim intNumbers () as integer = {2, 4, 6, 8, 10, 12}
Example of sorting an array
Dim intNumbers() as integer = { 7, 12, 1, 6, 3} array.sort(intNumbers)
Example of initializing a two dimensional array
Dim intNumbers(,) As Integer = { {1, 2, 3} , {4, 5, 6} , {7, 8, 9} } intNumbers(0, 0) is set to 1 intNumbers(0, 1) is set to 2 intNumbers(0, 2) is set to 3 intNumbers(1, 0) is set to 4 intNumbers(1, 1) is set to 5 intNumbers(1, 2) is set to 6 intNumbers(2, 0) is set to 7 intNumbers(2, 1) is set to 8 intNumbers(2, 2) is set to 9
Example of how to total values in a numeric array
Dim intTotal as integer = 0 For each intVal as integer in intUnits intTotal += intVal Next
Example of finding the lowest values in an integer array
Dim intUnits() as integer = {11, 32, 3, 44, 15} Dim intCount as integer Dim intLowest as integer intLowest = intUnits(0) For intCount = 1 to (intUnits.length-1) If intUnits(intCount) < intLowest then intLowest = intNumbers(intCount) End if Next
Example of finding the highest values in an integer array
Dim intUnits() as integer = {21, 12, 23, 14, 25} Dim intCount as integer Dim intHighest as integer intHighest = intUnits(0) For intCount = 1 to (intUnits.length-1) If intUnits(intCount) > intHighest then intHighest = intNumbers(intCount) End if Next
Example of a list of strings with a capacity of 3
Dim lastNames as new list(of string)(3)
Example of queue
Dim nameQueue As New Queue(Of String) nameQueue.Enqueue("Boehm") nameQueue.Enqueue("Murach") nameQueue.Enqueue("Vasquez") Dim nameQueueString As String = "" Do While nameQueue.Count > 0 nameQueueString &= nameQueue.Dequeue &vbCrLf Loop MessageBox.Show(nameQueueString, "Queue")
Example of stack
Dim nameStack As New Stack(Of String) nameStack.Push("Boehm") nameStack.Push("Murach") nameStack.Push("Vasquez") Dim nameStackString As String = "" Do While nameStack.Count > 0 nameStackString &= nameStack.Pop & vbCrLf Loop MessageBox.Show(nameStackString, "Stack")
Example of insert method
Dim phoneNumber as string "9775551212" phoneNumber = phoneNumber.Insert(3, "-") phoneNumber= phoneNumber.Insert(7,"-")
Example of a list of decimal elements
Dim prices as new list (of decimal)
Example of random numbers
Dim ran as new random For intCount = 0 to intMAX_SUBSCRIPT intNumbers(intCount) = rand.Next(100) Next
all values within an array are called
Elements
What adds the specified object to the end of the queue
Enqueue(Object) - method of Queue
Example of a for each loop with a listBox
For Each strCity as String in lstCities.Items If strCity = txtCity.Text then lblResult.Text = "The city was found!" End if Next
Example of getting the length of an array
For example Dim strNames () as String = {"Joe", "Geri", "Rose"} For intCount = 0 to strNames.length-1 messagebox.show(strNames(intCount)) Next
Example of returning an array from a function
Function GetNames() As String() Const intMAX_SUBSCRIPT As Integer = 2 Dim strNames(intMAX_SUBSCRIPT) As String Dim intCount As Integer For intCount = 0 To 3 strNames(intCount) = InputBox("Enter name " & (intCount.ToString()) Next Return strNames End Function
What gets the number of elements in the specified dimension of an array
GetLength(dimension) - instance method
What gets the index of the last element in the specified dimension of an array
GetUpperBound(dimension)- instance method
What inserts an element into a list at the specified index
Insert(index, object)
How can you store a value in an array element with an assignment statement
Int numbers (0) = 100 intNumbers (1)= 200 intNumbers(2) = 300 intNumbers(3) = 400 intNumbers(4) = 500 intNumbers(5) = 600
What gets or sets the element at the specified index and the index for the first item in a list is 0
Item(index)
What uses an index to access each element, efficient for accessing elements sequentially and inefficient for inserting elements into a list
List( Of T)
What if the default value of a string
Nothing
What returns the current date and time
Now - DateTime property
What gets the object at the top of the stack and removes it from the stack
Pop() - method of stack
Example of passing arrays as arguments
Private Sub DisplaySum(ByVal intArray() As Integer) Dim intTotal As Integer = 0 Dim intCount As Integer For intCount = 0 To (intArray.Length - 1) intTotal += intArray(intCount) Next MessageBox.Show("The total is " & intTotal.ToString()) End Sub Dim intNumbers() as integer = {2, 4, 7, 9, 8, 12, 10} displaysum(intNumbers)
Example of passing arrays by value and by reference
Private Sub ResetValues(ByVal intArray() as integer) Dim newArray() as integer = {0, 0, 0 ,0 ,0} intArray = newArray End Sub
What adds the specified object to the top of the stack
Push(object) - method of stack
What uses special methods to add and remove elements
Queue (Of T) and Stack (Of T)
What removes the first occurrence of the specified object from the list
Remove(object)
What removes the element at the specified index of a list
RemoveAt(index)
What sorts the elements in a list into ascending order
Sort()
What sorts the elements in a one dimensional array into ascending order
Sort(array)
What uses a key to access a value, which can be any type of object, efficient for inserting elements into a list and can be inefficient for accessing elements sequentially
SortedList (Of TKey, TValue)
What returns the characters from the Start position to the end of the string
StgringExpression.Substring(Start)
What starts at character position Start and searches Count characters for SearchString
StringExpr.IndexOf(SearchString, Start, Count)
What starts at character position Start and searches for SearchString from that point
StringExpression.IndexOf(SearchString, Start)
What searches entire string for SearchString
StringExpression.IndexOf(Searchstring)
What returns the number of characters specified by Length beginning with the Start position
StringExpression.Substring(Start, Length)
What converts the DateTime value to a string that includes the hours, minutes, and seconds
ToLongTimeString() - - DateTime methods
What converts the DateTime value to a string that includes the name for the day of week, the name for the month, the day of the month and the year
ToLongDateString() - DateTime methods
the name of an array is called what
array
what doesn't allow a statement to use a subscript outside the range called
array bounds checking
what stores multiple values of the same type
arrays
Example of processing array contents with Addition
intTallies(0) += 1
Why use an array
it is conveient
what must the upperbound be
it must be a positive integer
What gets the number of elements in all of the dimensions of an array
length- array property
Example of processing array contents with Format string
messagebox.show(decPay(5).tostring("c"))
What is an array with one subscript called
one dimensional array
what can a for each loop also be used for
processing items in a collection
How does the for each loop simplify array processing
retrieves the value of each element and cannot modify values
Example that inserts and removes an element from the list
salesTotals.Insert(0, 2745.73D) sales1 = salesTotals(0) Dim sales2 As Decimal = salesTotals(1) salesTotals.RemoveAt(1) sales2 = salesTotals(1)
Example that sorts and searches the list
salesTotals.Sort() Dim sales2Index as integer = salesTotals.BinarySearch(sales2)
You access the elements in an array through
subscript or index
what must elements be
they must be of the same data type
What is an array that is used to hold multiple sets of values
two dimensional array
the data type of the array is called what
type
what is the value of the array's highest subscript called
upperbound
the name of a variable just for use with the loop is called what
var