You are on page 1of 19

Array

Introduction

= useful containers that is used to organize variables and other information. A useful method for storing almost any amount of data during program execution Array also referred to as tables or subscripted variables Can help user to track a small set of values in ways that are impractical using traditional variables.

Creating an array

To create/declare an array is as simple as declaring simple variables. Scope of an array:

If an array is declared locally in a procedure, it can be used only in that procedure If an array is declared at the top of a form, it can be used throughout the form If an array is declared publicly in a module, it can be used anywhere in the project

Information in an array

Information in an array declaration statement Array name

Description

Used to present array in the program. In general, array names follow the same rule as variables names

Data type

Type of data stored in the array. In most cases, all the variables in the array are the same type.

Number of dimensions
Number of elements

Number of dimension array will contain. Most array are 1-D array & 2-D array but can specify additional dimension.

Number of elements array will contain. Subscript / index = inside the parentheses is the position

Fixed size Array

Basic syntax for a public fixed-size array :


Dim ArrayName(Dim1Index, Dim1Index, ) As DataType

Dim1Index = upper bound of the first dimension of the array, which the number of elements minus 1 Dim2Index = upper bound of the second dimension of the array, which the number of elements minus 1 Dim = keyword that declares the array. DataType = keyword corresponding to the type of data

Dim ArrayName(UpperSubScript) As DataType Dim ArrayName() As DataType = {InitialValueList} Dim ArrayName As DataType() = {InitialValueList}

Examples

Dim Employees(9) As String Dim Employees(0 To 9) As String Dim BalanceDecimal(10) As Decimal Dim Integer() As Integer = {1,5,12,18,20} Dim DepartmentsString As String() = {Accounting, Marketing} Private Employer(9) As String Friend EmployeesName(9) As String

Examples

Setting aside Memory


2-D array
Employee
0 1

Rows
0 1

Columns
0 1 2 4 3 4 5 6 7 8

2
3 4 5 Leslie

Scoreboard Dim Scoreboard (0 To 1, 0 To 8) As Short


Scoreboard (0 , 2) = 4

6
7 8 9

1-D array
Dim Employee (0, 9) As Short Employee (5) = Leslie

Multi Dimensional Array


Data is arranged in rows and columns To define 2-D array, the Dim statement specifies the number of rows and columns in the array Syntax :

Dim ArrayName (HighestRowSubscript, HighestColumnSubscript) As DataType Dim ArrayName( , ) As DataType = {ListOfValue}

Examples :

Dim nameString(2,3) As String Dim nameString( , ) As String = { {James,Mary,Sammie,Sean}, _ {Tom,Lee,Leon,Larry}, {Maria,Margaret,Jill,John},}

Dynamic Array

Are dimensioned at run time Either when the user specifies the size of the array Or when user add logic to the program determines an array size based on specific conditions User need to make reservations for the array at design

Steps

Specify the name and type of the array. Dim Temperatures () As Single Add code to determine the number of elements that should be in the array. You can prompt the user by using an InputBox function or a text box object. Dim Days As Short Days = InputBox(How many days?, Create array) Use the variable in a ReDim statement to dimension the array and subtracting 1. ReDim Temperatures(Days -1) Use the UBound function to determine the upper bound in a For..Next loop, and process the array elements as necessary. For i = 0 to UBound(Temperatures)
Temperatures(i) = InputBox(Prompt, Title)

Next

Examples

Array Class

Methods in the Array class of the Microsoft.NET Framework in used to sort, search and reverse elements in an array as well as perform other function Array class also provide a collection of methods that user can use to manipulate array Examples : Array.sort, Array.find, Array.reverse, Array.copy, Array.Clear

Examples

RandArray

Sort & Reverse

Array Cont.

The common theme to these is that they "collect together" pieces of related data. The most important collection class we will use in this module is the Array (which is also the simplest). An Array is a collection of objects all of the same type. Objects in an array can be accessed in any order

Example array

Example two dimensional array