• No results found

aarr1.ppt

N/A
N/A
Protected

Academic year: 2020

Share "aarr1.ppt"

Copied!
34
0
0

Loading.... (view fulltext now)

Full text

(1)
(2)

Objectives

After studying this chapter, you should be able to: • Declare and initialize a one-dimensional array • Store data in a one-dimensional array

• Determine the number of array elements and the highest subscript

• Traverse a one-dimensional array

• Code a loop using the For Each…Next statement

(3)

Objectives (cont'd.)

• Find the highest value in a one-dimensional array • Associate a list box with a one-dimensional array • Use a one-dimensional array as an accumulator • Sort a one-dimensional array

• Create and initialize a two-dimensional array • Store data in a two-dimensional array

(4)

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

(5)

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 one-dimensional array

– Starts at 0 for first element in the array

(6)

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

(7)
(8)

One-Dimensional Arrays (cont'd.)

• Each element in the array is initialized if no values are provided

– String array elements are initialized to the keyword Nothing

– Numeric array elements are initialized to 0

– Boolean array elements are initialized to False – Date array elements are initialized to 12:00 AM

(9)

One-Dimensional Arrays (cont'd.)

• Initial values can be specified for array elements

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

Length property: indicates number of elements

(10)
(11)

How to use a one-dimensional array’s Length property

(12)

How to use a one-dimensional array’s GetUpperBound method

(13)

Traversing a One-Dimensional Array

Traverse an array: look at each array element,

(14)
(15)

The For Each…Next Statement

For Each…Next statement:

– Used to code a loop that processes each element in a group or array

– Creates a variable used to represent each element in the group or array

(16)
(17)

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

(18)
(19)

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

(20)

Two-Dimensional Arrays (cont'd.)

• Two-dimensional array:

– 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

(21)
(22)

Two-Dimensional Arrays (cont'd.)

(23)
(24)
(25)

Traversing a Two-Dimensional Array

• To traverse a two-dimensional array, use two loops:

– Outer loop: tracks the row subscript

– Nested loop: tracks the column subscript

(26)
(27)
(28)
(29)

Calculate button’s Click event procedure

(30)

Dynamic Arrays

• You can re-dimension an array at run-time

• Rules for resizing arrays:

– Use can empty dimension list: Dim intArray() as Integer – Use ReDim to assign dimensions:

ReDim intArray (1 to intN, 1 to 2)

– Use ReDim to change Bounds

ReDim intArray (1 to (intN + 10), 1 to 3)

– Use Preserve keyword to save data in array

(31)

Summary

• Arrays: used to store related data in memory

• All variables in an array have the same name and data type

• To declare a one-dimensional array, provide the highest subscript or initial values

• One-dimensional array: each element is uniquely identified by its position (subscript) in the array

(32)

Summary (cont'd.)

• Length property: returns the number of elements in an array

• GetUpperBound method: returns the highest subscript in the array

• Use a loop to traverse a one-dimensional array • Can use variables in an array as accumulators

• Array.Sort method: sorts the elements in a one-dimensional array in ascending order

(33)

Summary (cont'd.)

• Two-dimensional array: each element is uniquely identified by its position (row and column

subscripts) in the array

• To declare a two-dimensional array, provide the highest row and column subscripts or initial values • Number of rows in a two-dimensional array is the

highest row subscript value + 1

(34)

Summary (cont'd.)

• Refer to an element in a two-dimensional array by using array name and element’s row and column subscripts separated by a comma

• Use a two-dimensional array’s GetUpperBound

method to determine the highest row subscript and the highest column subscript in the array

References

Related documents