CIS16 Application Development and Programming using Visual Basic.net Chapter Eight Arrays
Arrays
Variables Why use an array 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 Why use an array temporarily store related data in memory Increases the efficiency of a program
Arrays Variables in an array are used just like any other variables assign values to them Use them in calculations display their contents The most commonly used arrays in business applications are: one-dimensional like a rows of seats two-dimensional like many rows of seats
ONE-Dimensional Arrays
One-Dimensional Arrays The variables (elements) in an array are stored in consecutive locations in the computer’s main memory. You distinguish one variable in a one-dimensional array from another variable in the same array by using a unique number, called a subscript. The subscript: is always an integer indicates the variable’s position in the array is zero based The first variable in a one-dimensional array is assigned a subscript of 0, the second a subscript of 1, and so on Is specified in a set of parentheses immediately following the array name
One-Dimensional Arrays Declaring a One-Dimensional Array Use either {Dim | Private | Static} keywords Depends on whether you are creating a procedure-level array or a class-level array Must specify the data type, name, and highest subscript to be used Two syntax versions
One-Dimensional Arrays – syntax version 1 {Dim | Private | Static} arrrayName(highestSubscript) As data Type highestSubscript is an integer that specifies the highest subscript in the array. The first element in a one-dimensional array has a subscript of 0 and the array will contain one element more than the number specified in the highestSubscript argument. In other words, an array whose highest subscript is 2 will contain three elements.
One-Dimensional Arrays – syntax version 2 {Dim | Private | Static} arrrayName() As data Type={initialValues} initialValues is a comma-separated list of values you want assigned to the array elements. does not include the highestSubscript argument; instead, an empty set of parentheses follows the array name. The computer automatically calculates the highest subscript based on the number of values listed in the initialValues section.
Array Data types STRING data types If the array’s data type is String, each element is initialized using the keyword Nothing. Variables initialized to Nothing do not actually contain the word Nothing; rather, they contain no data at all. NUMERIC data types (Decimal,double, single, integer, long,short,byte) If the array’s data type is numeric, each element is initialized to the number zero (0) BOOLEAN data type (true or false) Elements in a Boolean array are initialized to the keyword “False”
Populating an array Assigning initial values to an array is called populating the array You can list the initial values in the initialValues section of the syntax, using commas to separate the values, and you enclose the list of values in curly braces { } After an array is declared, you can store the data in the array To enter data into an array, specify the subscript and use an assignment statement
Populating an array using equations or user provided data
Array size The Number of Elements in an Array is determined by examining the Arrays length property One way to determine the highest subscript is by subtracting the number 1 from the array’s Length property. The GetUpperBound method in the below figure returns an integer that represents the highest subscript in the specified dimension of the array.
Using length to determine highest subscript The highest subscript in a one-dimensional array is always one number less than its number of elements (its length) determine the highest subscript by subtracting the number 1 from the array’s Length property. This is useful to traverse an array using a loop
Using GetUpperBound to determine highest subscript The highest subscript in a one-dimensional array is always one number less than its number of array elements. The GetUpperBound method returns an integer that represents the highest subscript in the specified dimension of the array.
Determining the Highest Subscript in a One-Dimensional Array When used with a one-dimensional array, the specified dimension, which appears between the parentheses after the method’s name, is always 0.
Sorting a One-Dimensional Array Sort Ascending Array.Sort() method—sorts the values in a one-dimensional array in ascending order. Sort Descending first use the Array.Sort method to sort the values ascending then use the Array.Reverse method to reverse the sorted values.
Processing an array using the For Each..Next loop The For Each..Next loop is a convenient way of coding a loop whose instructions you want processed for each element in a group, such as for each variable in an array. code does not need to keep track of the array subscripts nor know the number of array elements. the instructions in a For Each...Next statement can only read the array values; they cannot permanently modify the values. the elementVariable that appears in the For Each .. Next clauses is any name you wish to give the variable that the computer will use to keep track of each element in the array . the variable’s data type is specified in the As dataType portion of the For Each clause -- it must be the same as the arrays’ data type.
Resizing an Array Redimensioning Syntax: change the size of one or more dimensions of an array that has already been declared Reallocates storage space for an array variable –free or acquire Syntax: ReDim [ Preserve ] name(boundlist) [ , name(boundlist) [, ... ] ] Preserve - preserve the data in the existing array when you change the size of only the last dimension. Name - Name of the array variable Boundlist - List of bounds of each dimension of the redefined array
PARALLEL one dimensional Arrays
Parallel One-Dimensional Arrays Two or more arrays whose elements are related by their position in arrays (by their subscripts) One-to-One correlation between the two Arrays may be different data types
Parallel One-Dimensional Arrays (cont.)
ARRAYS and collections
Arrays, loops and Collection Controls Collection controls and arrays are similar and frequently used together Similarities Groups of individual objects treated as one unit Each object is identified by a unique number The first index in a collection and the first array subscript are both 0 Items in a list box or combo box belong to the Collection class and can can be associated with arrays
Arrays, loops and Collection Controls
Two-Dimensional Arrays
Two-Dimensional Arrays 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
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
Declaring a Two-Dimensional Array – syntax version 1 {Dim | Private | Static} arrrayName(highestRowSub,highestColumnSub) As data type highestRowSub and highestColumnSub are integers that specify the highest row and column subscripts, respectively, in the array. When the array is created, it will contain one row more than the number specified in the highestRowSub argument and one column more than the number specified in the highestColumnSub argument. This is because the first row and column subscripts in a two-dimensional array are 0.
Declaring a Two-Dimensional Array – syntax version 2 {Dim | Private | Static} arrrayName(,) As data Type = {{initialValues},…{initialValues}} used when you want to specify each variable’s initial value. include a comma within the parentheses that follow the array’s name, indicating that the array is a two-dimensional array Include a separate initialValues section, enclosed in braces, for each row in the array, and within the individual initialValues sections, enter one or more values separated by commas and all of the initialValues sections must be enclosed in a set of braces. The number of values to enter corresponds to the number of columns in the array.
Storing data in Two-Dimensional Arrays After an array is declared, you can use another statement to store a different value in an array element. Examples of such statements include assignment statements and statements that contain the TryParse method.
Storing data in Two-Dimensional Arrays
Determining the highest subscripts in a Two-Dimensional Array
Traversing Two-Dimensional Arrays Use an outer loop and an inner nested loop. Outer loop: keeps track of the row subscript Nested inner loop: keeps track of the column subscript. For Each…Next loop can only read the array values cannot permanently modify the values
Traversing Two-Dimensional Arrays using loops
Traversing Two-Dimensional Arrays using For Each..Next
Two-Dimensional Arrays
Two-Dimensional Array Example (cont'd.)