0% found this document useful (0 votes)
18 views23 pages

CSC301 - Chapter 7

Uploaded by

dynzhaa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views23 pages

CSC301 - Chapter 7

Uploaded by

dynzhaa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Set up and use a control array

Establish an array to variables and


refer to individual elements in the
CHAPTER 7: array with variable subscripts

ARRAYS
Use the For Each/Next to traverse
the array

Store data in multidimensional array


INTRODUCTION TO ARRAYS

¡ An array variable is simply a variable that can store more


than one value
¡ Each individual item in array that contains a value is called an
element
¡ Arrays provide access to data by using a numeric index, or
subscript, to identify each element in the array
¡ An array declaration statement, includes the name
of the array, how many items it can store, and
what sort of data it can store.
¡ Must specify the number of array elements by
indicating the upper-bound index of the array.
¡ Upper-bound index specifies the index of the last
element of the array.
INITIALIZE
¡ Dimensioning array is setting the size of an
AN ARRAY array.
(1 OF 4)
INITIALIZE AN ARRAY (2 OF 4)

¡ You can declare an array by assigning values to each element.


¡ The array is implicitly sized when a number is not used in
the declaration statement to state the size of the array.
¡ Do not place an upper-bound index in the parentheses because
an error will occur.
INITIALIZE AN ARRAY (3 OF 4)

¡ Parallel arrays store related data in two or more arrays

strNames(0) strNames(1) strNames(2) strNames(3) strNames(4)


Baker Lopez Buck Chan Tirrell

intReservations(0) intReservations(1) intReservations(2) intReservations(3) intReservations(4)


4 5 12 2 8
INITIALIZE AN ¡ An array can be declared by specify its
ARRAY upper-bound index and assign each item of
(4 OF 4) the array one by one.
Data Type Default Value

All numeric data types 0

String data type Null

Boolean data type False

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

¡ Loop is used to reference each element of an array.


¡ The Visual Basic compiler determines if each
subscript is within the boundaries set when you
initialized the array.

ARRAY ¡ The exception occurs when the loop tries to reference


BOUNDARIES an element with the subscript 31.
¡ This element does not exist because the array contains
31 elements with an upper-bound index of 30.
UPPER-BOUND INDEX CONSTANT

¡ An array can use a constant value to represent its upper-bound


index.
¡ By using a constant, the size of several arrays can be specified
quickly.
REINITIALIZE AN ARRAY (1 OF 2)

¡ Every array in Visual Basic is considered dynamic, which means that


you can resize it at run time.
¡ When you change the number of elements in an existing array, you
re-dimension it.
¡ The ReDim statement assigns a new array size to the specified array
variable.
REINITIALIZE AN ARRAY (2 OF 2)

¡ 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)

¡ A two-dimensional array is like an array of arrays.


¡ A two-dimensional array holds data that is arranged in rows
and columns.

column 0 column 1 column 2 column 3


row 0 intVal(0,0) intVal(0,1) intVal(0,2) intVal(0,3)
row 1 intVal(1,0) intVal(1,1) intVal(1,2) intVal(1,3)
row 2 intVal(2,0) intVal(2,1) intVal(2,2) intVal(2,3)
CREATE A ¡ Nested loop is used to process two-
dimensional arrays.
TWO-
¡ The outer loop controls the column index and
DIMENSIONAL the inner loop controls the row index of the
ARRAY (2 OF 2) array.
END OF TOPIC …
ARRAYS

You might also like