Microsoft Visual Basic 2015: Reloaded Sixth Edition Chapter Nine Arrays
Objectives After studying this chapter, you should be able to: Declare and initialize one-dimensional and twodimensional arrays Store and access data in an array Determine the number of array elements and the highest subscript Traverse an array Code a loop using the For Each Next statement Compute the total and average of an array s contents 2
Objectives (cont'd.) Find the highest value in an array Associate a list box with a one-dimensional array Use a one-dimensional array as an accumulator or a counter Sort a one-dimensional array Search an array 3
Arrays Simple variable (or scalar variable): a variable that is unrelated to any other variable in memory Array A group of variables with the same name and data type that are related in some way Used to temporarily store related data in memory Increases the efficiency of a program Commonly used arrays: One-dimensional Two-dimensional 4
One-Dimensional Arrays One-dimensional array Can be viewed as a column of variables Each variable in the array has the same name and data type Subscript A unique number that identifies each variable in a onedimensional array Starts at 0 for first element in the array Use array name and subscript to refer to each individual variable in the array 5
One-Dimensional Arrays (cont d.) Element An individual variable in the array When an array is declared: Must specify the data type, name, and highest subscript to be used First element has a subscript of 0 The array will contain one element more than the highest subscript because subscripts start at 0 6
One-Dimensional Arrays (cont'd.) Figure 9-1: Illustration of the one-dimensional scientists array 7
One-Dimensional Arrays (cont'd.) Declaring a One-Dimensional Array Figure 9-2: How to declare a one-dimensional array 8
One-Dimensional Arrays (cont'd.) Populating the array Assigning initial values to an array List the values in a comma-separated list enclosed in curly braces ({}) After declaration, can use an assignment statement to store a value in an array element Figure 9-3: Illustration of the tvseries and rates arrays 9
One-Dimensional Arrays (cont'd.) Storing Data in a One-Dimensional Array Figure 9-4: How to store data in a one-dimensional array 10
One-Dimensional Arrays (cont'd.) Determining the Number of Elements in an Array Length property Indicates number of elements Figure 9-5: How to determine the number of elements in an array 11
One-Dimensional Arrays (cont'd.) Determining the Highest Subscript in a One- Dimensional Array GetUpperBound method Returns the highest subscript Figure 9-6: How to determine the highest subscript in a one-dimensional array 12
One-Dimensional Arrays (cont'd.) Traversing a One-Dimensional Array Look at each array element, one by one, from beginning to end of the array Use a loop to traverse an array 13 Figure 9-7: How to traverse a one-dimensional array
The For Each Next Statement For Each Next statement Used to code a loop that processes each element Creates a variable used to represent each Data type of the element must match the data type of the group 14 Figure 9-8 How to use the For Each Next statement
The For Each Next Statement (cont'd.) Calculating the Total and Average Values Figure 9-9: Problem specification, code, and sample run for the Popcorn Factory application (continues) 15
The For Each Next Statement (cont'd.) Figure 9-9: Problem specification, code, and sample run for the Popcorn Factory application 16
Finding the Highest Value Figure 9-10: Problem specification, code, and sample run another version of the Popcorn Factory application 17
Arrays and Collections Programmers associate the values in an array with the items in a list box Items in a list box belong to the Items collection and collections and arrays have several things in common Each is a group of individual objects treated as one unit Each individual object in the group is identified by a unique number (index or subscript) 18
Arrays and Collections (cont'd.) Figure 9-11: Problem specification, code, and sample run for the Presidents Vice Presidents application 19
Accumulator and Counter Arrays One-dimensional arrays are often used to either accumulate or count related values These arrays are commonly referred to as: accumulator arrays counter arrays 20
Accumulator and Counter Arrays (cont'd.) Figure 9-12 Problem specification, code, and sample run for the Allen School application (continues) 21
Accumulator and Counter Arrays(cont'd.) Figure 9-12 Problem specification, code, and sample run for the Allen School application 22
Sorting a One-Dimensional Array Sorting: arranging data in a specific order Ascending: first element is smallest, last element is largest Descending: first element is largest, last element is smallest Array.Sort method: used to sort elements in a one-dimensional array in ascending order Array.Reverse method: used after Array.Sort method to change to descending order 23
Sorting a One-Dimensional Array (cont'd.) 24 Figure 9-13: How to use the Array.Sort and Array.Reverse methods
Two-Dimensional Arrays Two-dimensional array Resembles a table with rows and columns Each element is identified by a unique combination of two subscripts: (row, column) Subscripts are zero-relative Refer to an element using the name followed by the (row, column) pair in parentheses Figure 9-14: Names of some of the elements in the songs array 25
Two-Dimensional Arrays (cont'd.) Two-dimensional array are declared with highest row subscript and highest column subscript (zero-relative) Number of rows = highest row subscript + 1 Number of columns = highest column subscript + 1 Can specify initial values for array elements If no initial values are declared, array elements are automatically initialized 26
Two-Dimensional Arrays (cont'd.) Figure 9-15: How to declare a twodimensional array 27
Two-Dimensional Arrays (cont'd.) Figure 9-16: How to store data in a two-dimensional array 28
Two-Dimensional Arrays (cont'd.) Figure 9-17: How to determine the highest subscripts in a two-dimensional array 29
Two-Dimensional Arrays (cont'd.) Traversing a Two-Dimensional Array Use two loops Outer loop: tracks the row subscript Nested loop: tracks the column subscript Can also use a For Each Next loop 30
Two-Dimensional Arrays (cont'd.) Figure 9-18: How to traverse a twodimensional array 31
Two-Dimensional Arrays (cont'd.) Totaling the Values Stored in a Two-Dimensional Array Figure 9-19: Problem specification, code, and sample run for the Tyler Motors application (continues) 32
Two-Dimensional Arrays (cont'd.) Totaling the Values Stored in a Two-Dimensional Array Figure 9-19: Problem specification, code, and sample run for the Tyler Motors application (continues) 33
Two-Dimensional Arrays (cont'd.) 34 Figure 9-19: Problem specification, code, and sample run for the Tyler Motors application
Two-Dimensional Arrays (cont'd.) Searching a Two-Dimensional Array Figure 9-20: Problem specification, code, and sample run for the O Reilly Studios application (continues) 35
Two-Dimensional Arrays (cont'd.) 36 Figure 9-20: Problem specification, code, and sample run for the O Reilly Studios application
Programming Tutorial 1 Figure 9-21: TOE chart and MainForm for the Lottery Game application Figure 9-22: Pseudocode for the getbutton_click procedure Figure 9-24: Sample run of the Lottery Game application 37
Programming Tutorial 2 Figure 9-26: Membership fees, TOE chart, and MainForm for the Jenkins Gym application Figure 9-27: Sample run of the Jenkins Gym application 38
Programming Example Figure 9-29: TOE Chart 39 Figure 9-30: MainForm and tab order
Summary Programmers use arrays to temporarily store related data in the internal memory of the computer All of the elements in an array have the same name and data type When declaring a one-dimensional array, you provide either the highest subscript or the initial values Each element in a one-dimensional array is identified by a unique subscript that appears in parentheses after the array s name 40
Summary (cont'd.) You refer to an element in a one-dimensional array using the array s name followed by the element s subscript, which is enclosed in parentheses Examples of statements that you can use to change the data stored in an array include assignment statements and statements that contain the TryParse method A one-dimensional array s Length property contains an integer that represents the number of elements in the array 41
Summary (cont'd.) A one-dimensional array s GetUpperBound method returns an integer that represents the highest subscript in the array You use a loop to traverse a one-dimensional array You can associate the items in a list box with the elements in an array You can use the elements in an array as accumulators or counters 42
Summary (cont'd.) The Array.Sort method sorts the elements in a one-dimensional array in ascending order The Array.Reverse method reverses the order of the elements in a one-dimensional array A two-dimensional array resembles a table in that the elements are in rows and columns When declaring a two-dimensional array, you provide either the highest row and column subscripts or the initial values 43
Summary (cont'd.) The number of rows in a two-dimensional array is one number more than its highest row subscript You can determine the number of elements in a twodimensional array by multiplying the number of its rows by the number of its columns. You can also use the array s Length property Each element in a two-dimensional array is identified by row and column subscripts 44
Summary You can use a two-dimensional array s GetUpperBound method to determine the highest row subscript and column subscript in the array You can traverse a two-dimensional array using either two loops (coded with the For Next or Do Loop statements) or one loop (coded with the For Each Next statement) 45