2. 2
Introducing Arrays
• Each array contains data using same variable
name and data type
• Each value or object stored at a position
– Position numbers start at zero
• Zeroth element is first element
– Position also called index or subscript
– Index must be zero, a positive Integer, or an Integer
expression
3. 3
Introducing Arrays
Name of array (note that
all elements of this array
have the same name,
intNetUnitsSold)
Position number (index
or subscript) of the
element within array
intNetUnitsSold
intNetUnitsSold(0)
intNetUnitsSold(1)
intNetUnitsSold(2)
intNetUnitsSold(3)
intNetUnitsSold(4)
intNetUnitsSold(5)
intNetUnitsSold(6)
intNetUnitsSold(7)
intNetUnitsSold(8)
intNetUnitsSold(9)
intNetUnitsSold(10)
intNetUnitsSold(11)
0
10
16
72
154
89
0
62
-3
90
453
178
78intNetUnitsSold(12)
Array consisting of 13 elements
5. 5
Declaring and Allocating Arrays
• You must specify the size, name and data type
for an array before using
• Allocating memory for the array
– Use New to create the array
– Initializer list
• Specifies initial values of elements
7. 7
Declaring and Allocating Arrays
• GetUpperBound method
– Returns highest index in the array
• Length property
– Returns the length of the array
8. 8
Declaring and Allocating Arrays
Calculating the sum of the values of an array’s elements.
Retrieve the value of
each element and add
it to the total, one at a
time
11. Two-Dimensional Rectangular
Arrays
• Two-dimensional arrays
– Also called double subscripted
– Requires two indices to identify particular elements
– Often used to arrange tables into rows and columns
– m-by-n array has m rows and n columns
12. Row 0
Row 1
Column 0 Column 1 Column 2 Column 3
Column index (or
subscript)Row index (or subscript)
Row 2
intA rray(0,0) intA rray(0,1) intA rray(0,2) intA rray(0,3)
intA rray(1,0) intA rray(1,1) intA rray(1,2) intA rray(1,3)
intA rray(2,0) intA rray(2,1) intA rray(2,2) intA rray(2,3)
Array name
Figure 18.4 Two-dimensional rectangular array with three rows and four columns.
Two-Dimensional Rectangular
Arrays
13. 13
Two-Dimensional Rectangular
Arrays
• Array declaration
– Dim intNumbers As Integer(,) = New Integer(1, 1) {}
intNumbers(0, 0) = 1
intNumbers(0, 1) = 2
intNumbers(1, 0) = 3
intNumbers(1, 1) = 4
OR
– Dim intNumbers As Integer(,) =
New Integer(,) {{1, 2}, {3, 4}}