Arrays
LESSON CONTENT
• ARRAY
• INITIALIZING ARRAY
• DYNAMICAL ARRAY
• COMMONLY USED METHODS IN THE ARRAY CLASS
Lesson Objectives
• Understand how to declare an array,
initialize an array, and refer to individual
elements of an array.
• Analyze and share opinions on how array
works in a program
• Declare and manipulate one-dimensional array
Lesson Review
• What is a Variable?
• A program that needs user’s input requires
location or storage in the computer’s memory.
For that matter, variables are being used to
hold the values or data. However, a variable
can only hold one value at a time. To store
more information with similar data types, you
have to use variable for each item.
• For example, if your program should accept
more than 20 different employee names,
you should declare 20 different variables as
well. You can use ARRAY instead of
declaring variables.
WHAT IS AN ARRAY
• An array is a group of contiguous memory locations that
have the same name and the same type.
• It can be
• ONE DIMENSIONAL – Contains one row of values only
• MULTIDIMENSIONAL – contains multiple rows of values and
requires two or more indices to identify particular elements.
EXAMPLE
Dim EMPName(4) as String
EMPName (0) = “ Lara”
EMPName (1) = “Resel”
EMPName (2) = “Amir”
EMPName (3) = “Kaztiel”
EMPName (4) = “Angela”
• SUBSCRIPT / INDEX – indicates the specific location of
values in an array.
It always begin/start counting at 0. The first element
is called ZEROTH element. An array declaration with
specified number of subscript is called FIXED-SIZED
ARRAY.
Example Explained
• In the five lines, all started with variable name EMPName,
followed by the element number, then the assigned values.
The format of declaring an array variable is:
Dim|Public|Private ArrayName(Subscript) as
Datatype
ACTIVITY
• Create a console application that will display the values of
an array variable using a message box.
Sub Main()
Dim EMPName(4) As String
EMPName(0) = " Lara"
EMPName(1) = "Resel"
EMPName(2) = "Amir"
EMPName(3) = "Kaztiel"
EMPName(4) = "Angela"
MsgBox("The first name in the array is " & EMPName(0) & vbCrLf _
& "The second name in the array is " & EMPName(1) & vbCrLf _
& "The third name in the array is " & EMPName(2) & vbCrLf _
& "The fourth name in the array is " & EMPName(3) & vbCrLf _
& "The fifth name in the array is " & EMPName(4), vbOKOnly, _
"Displaying the Values in an Array Variable")
Console.ReadKey()
End Sub
Module Module1
Sub Main()
Dim EMPName() As String = {" Lara", "Resel", "Amir", "Kaztiel",
"Angela"}
ReDim Preserve EMPName(6)
MsgBox("The first name in the array is " & EMPName(0) & vbCrLf
_
& "The second name in the array is " & EMPName(1) &
vbCrLf _
& "The third name in the array is " & EMPName(2) & vbCrLf _
& "The fourth name in the array is " & EMPName(3) & vbCrLf _
& "The fifth name in the array is " & EMPName(4) & vbCrLf _
& "The sixth name in the array is " & EMPName(5) & vbCrLf _
& "The seventh name in the array is " & EMPName(6) & vbCrLf _
, vbOKOnly, "Displaying the Values in an Array Variable")
Console.ReadKey()
End Sub
End Module
• REMOVE the Word “PRESERVE” on variable declaration
and Run the program
Sub Main()
Dim EMPName() As String = {" Lara", "Resel", "Amir", "Kaztiel",
"Angela"}
ReDim EMPName(6)
MsgBox("The first name in the array is " & EMPName(0) & vbCrLf _
& "The second name in the array is " & EMPName(1) & vbCrLf _
& "The third name in the array is " & EMPName(2) & vbCrLf _
& "The fourth name in the array is " & EMPName(3) & vbCrLf _
& "The fifth name in the array is " & EMPName(4) & vbCrLf _
& "The sixth name in the array is " & EMPName(5) & vbCrLf _
& "The seventh name in the array is " & EMPName(6) & vbCrLf _
, vbOKOnly, "Displaying the Values in an Array Variable")
Console.ReadKey()
End Sub
SEARCH FOR ARRAY
ELEMENTS
Dim EMPName() As String = {" Lara", "Resel", "Amir", "Kaztiel",
"Angela"}
Public Usrinput As String
Sub Main()
Usrinput = InputBox("Enter as Valid name: ", "User
Validation")
If EMPName.Contains(Usrinput) Then
MsgBox(Usrinput & " is in the list", vbOKOnly,
"Valid Account")
Else
MsgBox(Usrinput & " is NOT in the list", vbOKOnly,
"Invalid Account")
End If
Console.ReadKey()
Sort Array Values
Module Module1
Dim EMPName() As String = {"Lara", "Resel", "Amir", "Kaztiel", "Angela"}
Public Usrinput As String
Sub Main()
Usrinput = StrConv(InputBox("Enter as Valid name: ", "User
Validation"), VbStrConv.ProperCase)
If EMPName.Contains(Usrinput) Then
MsgBox(Usrinput & " is in the list", vbOKOnly, "Valid Account")
SortString()
Console.ReadKey()
Else
MsgBox(Usrinput & " is NOT in the list", vbOKOnly, "Invalid
Account")
End If
End Sub
Sub SortString()
Dim value As String
Array.Sort(EMPName)
For Each value In EMPName
Console.WriteLine(value)
Next
Console.ReadKey()
End Sub
QUESTIONS?
1. What is an array?
2. When is the best time to use
Arrays?
ACTIVITY TIME
Write a program that calculate the average of 10 students.
Use 2 arrays ( 1 array for student names and 1 array for
their grades)