meljun cortes array

Post on 22-May-2015

97 Views

Category:

Technology

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

MELJUN CORTES ARRAY

TRANSCRIPT

MELJUN CORTESMELJUN CORTES

A simple or scalar variable is one that is unrelated to any other variable in memory

An array is a group of variables that have the same name and data type and are related in some way

The most commonly used arrays are one-dimensional and two-dimensional

Programmers use arrays to store related data in the internal memory of the computer

If you declare an array locally in a procedure, you can use it only in that procedure.

If you declare an array at the top of a form, you can use it throughout the form.

If you declare an array publicly in a module, you can use it anywhere in the project.

Syntax Elementsin Array Declaration

Description

Array name The name you’ll use to represent your array in the program. In general,array names follow the same rules as variable names.

Data type The type of data you’ll store in the array. In most cases, all thevariables in an array are the same type.

Number of dimensions

The number of dimensions that your array will contain. Most arrays areone-dimensional (a list of values) or two-dimensional (a table of values),but you can specify additional dimensions

Number of elements

The number of elements that your array will contain. The elements inyour array correspond directly to the array index. The first array indexis always 0 (zero).

For example, to declare a one-dimensional string array named Employees that has room for 10 employee names (numbered 0 through 9), you can type the following in an event procedure:

Dim Employees(9) As String

Public Employees(9) As String

Dim Employees(0 To 9) As String

To declare a public two-dimensional array named Scoreboard that has room for two rows and nine columns of Short integer data, you can type this statement in an event procedure or at the top of the form::

Dim Scoreboard(1, 8) As Short

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

To refer to an element of an array, you use the array name and an array index enclosed in parentheses.

Employees(5) = "Leslie"

To refer to an element of an array, you use the array name and an array index enclosed in parentheses.

Scoreboard(0, 2) = 4

To create an array in this manner, you use what is called an array literal

An array literal consists of a list of comma-separated values that are enclosed in braces ({}).

A subscript must reference a valid element of the array. lf a list contains 10 names it wouldn't make sense to ask: What is the 15th name on the list? Or What is the 21/2th name on the list? Visual Basic rounds fractional subscripts and throws an exception for a subscript that is out of range.

Each individual variable is called an element of the array.

Note: Arrays are based on System.Array, which is a collection.

Fixed Array program uses the UBound function to check for the upper bound, or top index value, of the array.

LBound(ArrayName)UBound(ArrayName)

•LBound function, which confirmsthe lower index value, or lower bound, of

LBound function, which confirms the lower index value, or lower bound, of an array, is

Dynamic arrays are dimensioned at run time, either when the user specifies the size of the array or when logic you add to the program determines an array size based on specific

ReDim statement specifies the size of a dynamic array at run time

If you redimension an array that already has data in it, all the existing data is irretrievably lost

Dim Temperatures() As SingleDim Days As ShortDays = InputBox("How many days?", "Create Array")ReDim Temperatures(Days - 1)

Allows you to preserve the data in an array when you change its dimensions.

Dim Philosophers() As Stringyou can redimension the array and add data to it by using code similar to the following:ReDim Philosophers(200)Philosophers(200) = "David Probst"You can expand the size of the Philosophers array to 301 elements (0–300), and preserve the existing contents, by using the following syntax:ReDim Preserve Philosophers(300)

To use an array to store items that are related but have different data types.

One solution is to use two parallel one-dimensional arrays: a String array to store the IDs and an Integer array to store the salaries.

Parallel arrays are two or more arrays whose elements are related by their position in the arrays.

top related