CSC301 - Chapter 7
CSC301 - Chapter 7
ARRAYS
Use the For Each/Next to traverse
the array
INITIALIZE AN
¡ Each element is assigned a default value
ARRAY WITH
when you initialize an array but do not
DEFAULT assign values immediately.
VALUES
ACCESS ARRAY ELEMENTS USING A LOOP
¡ When you used the ReDim statement to re-dimension the array, all
data contained in the array is lost.
¡ If you want to preserve the existing data, you can use the keyword
Preserve.
USE THE
LENGTH ¡ The Length property of an array contains
PROPERTY the number of elements in an array.
(1 OF 2)
USE THE LENGTH PROPERTY (2 OF 2)
¡ Using the Length property can prevent the program from throwing
the IndexOutOfRange exception.
¡ From the example, For loop uses the Length property to determine
the number of loop iterations.
¡ For Each loop is a special loop designed
THE FOR specially for arrays.
EACH LOOP
¡ The For Each loop cycles through each
(1 OF 2) array element until the end of the array.
¡ Each element in the array is assigned to the control variable
strPioneer as the For Each loop is executed.
THE FOR EACH ¡ The array elements and the control variables are both
String.
LOOP (2 OF 2) ¡ When the loop begins, the first element of the
strFamousComputerPioneers array is placed in the
strPioneer variable and the body of the loop is executed.
¡ The looping continues until all elements within the array have
been processed.
¡ The scope of an array declared within a
procedure is local to that procedure.
SCOPE OF
¡ An array can be declared as a class-level variable
ARRAYS and the array is visible to all procedures within
the class.
¡ An array can be passed as an argument to a
PASS AN Sub procedure or a Function procedure.
ARRAY ¡ If you change the value of any array element
in a procedure, the original array is changed.
¡ You can use the Array.Sort method to sort the
values in an array in ascending order.
SORT AN ¡ To sort the values in descending order, you first use
ARRAY the Array.Sort method to sort the values in
ascending order; then use the Array.Reverse
method to reverse the sorted values.
¡ Searching each element in an array is called a
sequential search.
¡ The BinarySearch method searches a sorted array
for a value using a binary search algorithm.
¡ The binary search algorithm searches an array by
repeatedly dividing the search interval in half.
SEARCH AN
ARRAY
CREATE A TWO-DIMENSIONAL ARRAY (1 OF 2)