Introduction To Arrays - 2021
Introduction To Arrays - 2021
Arrays
Ghana Technology University College
Lecturer – Dr. Forgor Lempogo
2021
Objectives
At the end of this lesson, you should be able to:
Create and manipulate a one-dimensional array
Array:
A group of variables with the same name and data type
that are related in some way
Used to temporarily store related data in memory
Increases the efficiency of a program
Subscript/Index:
A unique number that identifies each variable in arrays
Starts at 0 for first element in the array
Must be an integer
Element
Individual item in the array
5
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Referencing Array Elements
Use the Index(s) of the Element
strName
strName(0) = "Ama Dela"
(0) Ama Dela
(1) Kwaku Menu strName(1) = "Kwaku Menu"
(2) John Mensah strName(2) = "John Mensah"
(3) Dela Kuma strName(3) = "Dela Kuma"
6
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Manipulating One-Dimensional Arrays
Array elements can be used like any other variable
Examples:
Display the contents of an array
Access an array element using its subscript
Search the array
Calculate the average of data stored in a numeric array
Find the highest value stored in an array
Update array elements
Sort array elements
7
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
A One-Dimensional Array - Example
8
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
A One-Dimensional Array - Example
Private Sub MainForm_Load( ByVal sender As Object, ByVal e As System.EventArgs} Handles
Me.Load
‘Fill the list box with array values and the select the first item
Dim months () As String = { "JAN" , "FEB" , "MAR" , "APR" , "MAY" , "JUN" , “JUL ", “AUG" ,
"SEP“, “OCT" , "NOV" , "DEC”}
For subscript As Integer = 0 To 11
monthListBox.Items.Add(months(subscript))
Next subscript
monthListBox.Selectedindex = 0
End Sub
10
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
The For Each…Next - Example
Syntax:
For Each element [As datatype] In group
[statements]
Next element
Examples:
For Each monthName As String In months
monthListBox.Items.Add(monthName)
Next monthName
codeTextBox.SelectAll()
End Sub
salaryLabel.Text = String.Empty
End Sub
14
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
One-Dimensional Array – Example 3
Agent Ama Ali John Abena Kofi
Amount 45000 35000 25000 60000 23000
21
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
One-Dimensional Array – Example 5
Module Level Declarations
Private regionNames(9) As String
22
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
One-Dimensional Array – Example 5
Ascending Button Click
Array.Sort(regionNames)
lblRegion.Text = String.Empty
For Each name As String In regionNames
lblRegion.Text = lblRegion.Text & name & _
ControlChars.NewLine
Next name
27
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Parallel One-Dimensional Arrays Example
idTextBox.SelectAll()
End Sub
idTextBox_TextChange Event
Private Sub idTextBox_TextChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles idTextBox.TextChanged
priceLabel.Text = String.Empty
End Sub
30
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Arrays Example 7
A Visual Basic Application that
will accept a list of students
and their marks as input using
parallel one dimensional
arrays.
35
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Show the Best Student
37
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Show Last student
39
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
ReDim Statement
After an array has been declared, its size (but not its type)
can be changed with a statement of the form:
ReDim arrayName(m)
Where:
arrayName is the name of the already declared array
m is an Integer literal, variable, or expression.
Note:
Since the type cannot be changed, there is no need for an
"As dataType" clause at the end of the ReDim statement
42
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
ReDim Statement - Shortcomings
It causes the array to lose its current contents.
it resets all String values to “”
resets all numeric values to 0.
44
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
ReDim Preserve Statement
The general form:
Examples
ReDim Preserve strDept(20)
ReDim Preserve decSale(6)
45
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Two-Dimensional Arrays
Two-dimensional array:
Resembles a table with rows and columns
46
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Two-Dimensional Arrays (2)
Examples:
Dim cities(5 , 3) As String
declares a six-row, four-column array named cities; each
element is automatically initialized using the keyword Nothing
Dim scores( , ) As Integer = {{ 75, 90} , {9 , 25} ,{23 , 56} ,{6 , 12}}
Examples:
cities(0, 0) - "Tamale"
cities(0, 1) - "Sunyani"
cities(0, 2) - "Techiman"
cities(0, 3) - "Accra"
idTextBox . SelectAll()
End Sub
End Sub
54
CICS 313: Visual Basic Programming - GTUC 2021 Delivery
Any Questions?