CSCI1580 Lab 7
Array & 2D Array
Shaozuo YU
szyu22@cse.cuhk.edu.hk
Objective
• Usage of array
– Declaration and allocation of array
– Accessing elements in array
What is array?
• An array is a group of variables.
a single variable array with 8 variables
C -45 C(0) -45
C(1) 6
C(2) 7
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
Note: The first element has the index of zero!
Declaration and Allocation
• To declare an array, we haveIndicate
to provide
that the name
this is a array
and the type:
Dim c As Integer() or Dim c() As Integer
Arrays are objects, so they must
Allocate memory be allocated using the
Initialization
keyword New:for the array of the array
c = New Integer(11) {} or c = New Integer(0 To 11) {}
The last index
of the array An array with 12 variables.
These two steps can be shortened:
Dim c As Integer() = New Integer(11) {}
Dim c(11) As Integer
Array Length
• c.Length is the size of array c.
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.
Dim c(11) As Integer
c.Length = 12
c.GetUpperBound(0) = 11
For Each … In
• Alternatively, we can use for each statement to access each
element
Sub Main()
' Create an array with 6 elements
' All elements are initialized
Dim c As Integer() = {10, 9, 8, 7, 6, 5}
For Each element in c
Console.WriteLine(element)
Next
End Sub
10
9
8
7
6
5
Exercise
• Declare two array
• Allocate two array
• Accessing elements in two array with the
same index
2D Array
• Definition: A 2D array is a collection of data items,
all of the same type, which are accessed using two
indices, one to select the row and the other to
select the column.
• Importance: 2D arrays are crucial for handling
tabular data, matrix operations, and creating grid-
based games.
• Real-world Application: Examples include storing
data of a spreadsheet, pixel values of an image, or
the board state in a game like chess.
Review Declaration and Initialization
Declare with Initializers
Dim a As Integer(,) = New Integer(1,2) {{1,2,3}
{4,5,6}}
Short Version:
Dim a As Integer(,) = {{1,2,3}{4,5,6}}
Accessing 2D Array Elements
Accessing Elements: Elements in a 2D array are
accessed using two indices: 2DArray(row, column)
Looping: Nested loops are used to traverse 2D arrays.
Operations on 2D Arrays
Common Operations: Adding/removing rows or
columns, finding the dimensions, etc.
Example Code for Finding Dimensions:
Execises
1. Create and initialize a 2D array to represent a 3x3
matrix.
2. Write a program to find the maximum and minimum
values in a 2D array.
To review 2D array as Function return variable, parameter and
augumentation. Please check the Lecture Note 09. 2D Array P6-P7
Thanks