6. Array Example nameString Array (0) (1) (2) (3) (4) (5) (6) (7) (8) (9) Janet Baker George Lee Sue Li Samuel Hoosier Sandra Weeks William Macy Andy Harrison Ken Ford Denny Franks Shawn James
7.
8.
9.
10.
11.
12.
13. The For Each and Next Statements Example For Each eachNameString In n ameString ' Write one element of the array. Debug.WriteLine(eachNameString) Next eachNameString
14.
15.
16.
17.
18.
19.
20. ReDim Code Example ' Module-level declarations. Structure SalesDetail Dim saleDecimal( ) As Decimal End Structure Private houseWaresSalesDetail As SalesDetail ' Inside a procedure. ' Establish the number of elements in the array. ReDim houseWaresSalesDetail.saleDecimal(6) ' In processing. houseWaresSalesDetail.saleDecimal _ (dayIndexInteger) = currentDaySalesDecimal
27. Using List Boxes With Arrays (1 of 2) groupNumberInteger = groupListBox.SelectedIndex Allow the user to select from a list and the SelectedIndex property can be used as the subscript of the total array.
28.
29. The Dim Statement for Two-Dimensional Arrays - General Form Dim ArrayName ( HighestSubscript, Highest Subscript ) As Datatype Dim ArrayName ( , ) As Datatype = {ListOfValues}
30.
31.
32. Nested For/Next Example For rowInteger = 0 To 2 For columnInteger = 0 To 3 ' Initialize each element. nameString(rowInteger, columnInteger) = " " Next columnInteger Next rowInteger
33. Printing a Two-Dimensional Table ' Print one name per line. For Each elementString In nameString ' Set up a line. e . Graphics . DrawString(elementString, printFont, _ Brushes.Black, horizontalPrintLocationSingle, _ verticalPrintLocationSingle) ' Increment the Y position for the next line . verticalPrintLocationSingle += lineHeightSingle Next elementString
Brief description on how to navigate within this presentation (ppt) The first time a Key Term from the chapter is used in the ppt it will display in blue Gold colored text boxes display coding examples Slides will be numbered (# of #) when multiple slides on same topic (Slide title) Speaker notes are included where appropriate for slides (*)Denotes either a comment for page reference to textbook or slide reference in ppt
The real advantage of using an array is not realized until you use variables for subscripts in place of constants
Arrays can be declared by using Dim, Public, Private, or Friend keyword Just as with any other variable, the location of the declaration determines the scope and lifetime of the array variables *The next slide displays the Declaration Statements for Arrays Example
Array subscripts are zero bases, so the first element is always element zero The upper subscript is the highest subscript—one less than the number of elements You declare a data type for the array—all of the array elements must be the same data type; if the data type is omitted by having Option Strict off, just as single variables, the type defaults to Object
Arrays are based on System.Array, which is a collection
The significant advatanges of using the For Each and Next (key term) are that subscripts for the array don’t have to be manipulated or the user doesn’t need to know how many elements there are in the array *The next slide displays the General Form for the For Each and Next Statements
If the array has 12 elements, the loop will execute 12 times *The next slide provides an example of the For Each and Next Statements
The For Each loop executes if the array has at least one elements—all the statements within the loop are executed for the first element If the array has more elements, the loop continues to execute until all the elements are processesd
*The next slide displays the general form for the Structure and End Structure Statements
By default, a structure is public—it can be declared to be Friend or Private if desired
*Continuation of the Structure and End Structure Statement Example is on the next slide
Once a structure is created variables of the structure can be declared just as if it were another data type
A variable that is not an array, does not need an index
*The next slide provides an example of the code used to include an Array in a Structure
Array elements are regular variables and perform in the same ways as many other variables Subscripted variables can be used in any choosen such as for counters or total accumulators *Use example in the textbook on p. 327 to explain this slide
Things don’t always work out so neatly as having sequential group numbers that can be used to access the table directly Sometimes a little work is involved to find (look up) the correct value and reference the array elements indirectly
For a table lookup, a Do/Loop works better than For Each When comparing to element in the array and eventually finding a match the user needs to know the subscript of the matching element *Example on p. 331 in textbook
The table-lookup technique will work for any table, numeric or string It isn’t necessary to arrange the fields being searched in any particular sequence The comparison is made to one item in the list, then the next, and the next until a match is found Time can be saved in a large table by arranging the elements with the most-often-used entries at the top so that fewer comparisons must be made
*The next slide displays an example of using a List Box
Generally two subscripts are needed to identify tabular data, where data is arranged in rows and columns
The elements of the array may be used in the same ways as any other variable – in accumulators, counts and reference fields for lookup, or in statements like assignment and printing, and as conditions
Numeric array elements are initially set to 0 and string elements are set to empty strings; initial values can be assigned when declaring the array
The assignment statement will be executed once for each element of nameString
The sum of a table can be found in various ways; either sume the columns or the rows of the table; or, as sum the figures in both directions and double-check the totals; to sum an array in both directions, each column needs one total variable and each row needs one total variable Two-one dimensional arrays work well for the totals
A table lookup is the most common lookup technique
*This example is found in the textbook on p. 337 Many two-dimensional tables used for lookup require additional one-dimensional arrays or lists to aid in the lookup process