Documente Academic
Documente Profesional
Documente Cultură
Each element in the array is referenced using the array name and an index.
Value in array element
Array a
a[1]
a[2]
a[3]
a[4]
a[5]
11
12
4
16
3
Element value
Last value is in a(5)
In code, to access each element of the array, that is, to access each value in the array, use a
For loop. Use the loop variable as the index value in the array.
For i = 1 to 5
lstOutput.additem ( a(i) )
next
2 D arrays
Arrays can have multiple dimensions.
You can use a two-dimensional array to represent a matrix or a table. For example, the
following table that describes the distances between the cities can be represented using a twodimensional array.
matrix( i, j)
row
col
' Assume that array a has been initialised with a set of values for each element in the array.
for i = 1 to 4 ' # of rows
for j = 1 to 3 ' # of columns
lstOutput.additem( a(i,j) )
Inner loop
next j
next i
a(3,2)
Column
1
Row
1
2
3
4
a(i,j)
1
2
3
1
2
3
1
2
3
1
2
3
a(1,1)
a(1,2)
a(1,3)
a(2,1)
a(2,2)
a(2,3)
a(3,1)
a(3,2)
a(3,3)
a(4,1)
a(4,2)
a(4,3)
Iteration
of outer loop