CSCI1580-Lab6-Tutorial
CSCI1580-Lab6-Tutorial
Shaozuo YU
[email protected]
Objective
• Usage of array
– Declaration and allocation of array
– Accessing elements in array
What is array?
Name of array. C
C(3) 1534
C(4) 0
C(5) 1
C(6) 78
Index (or subscript) of the
element in array C. C(7) 999
Sub Main()
' Create an array with 6 elements Length of c = 6
' All elements are initialized 0 10
Dim c As Integer() = {10, 9, 8, 7, 6, 5} 1 9
Console.WriteLine(“Length of c = " & c.Length) 2 8
For i As Integer = 0 To c.Length-1 Step 1 3 7
Console.WriteLine(i & " " & c(i)) 4 6
Next 5 5
End Sub
Array Upper Bound
Recommended!
• c.GetUpperBound(0) returns the index of the last
element in the array, i.e., one less than the length.
Sub Main()
' Create an array with 6 elements c.Length-1
' All elements are initialized
Dim c As Integer() = {10, 9, 8, 7, 6, 5}
Console.WriteLine(“GetUpperBound(0) = " & c.GetUpperBound(0))
For i As Integer = 0 To c.GetUpperBound(0) Step 1
Console.WriteLine(i & " " & c(i))
Next
End Sub GetUpperBound(0) = 5
0 10
1 9
2 8
3 7
4 6
5 5
Array Upper Bound
• An array with 12 variables.
Short Version:
Dim a As Integer(,) = {{1,2,3}{4,5,6}}
Accessing 2D Array Elements