meljun cortes array discussion

Upload: meljun-cortes-mbampa

Post on 14-Apr-2018

220 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/27/2019 MELJUN CORTES Array Discussion

    1/24

  • 7/27/2019 MELJUN CORTES Array Discussion

    2/24

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

    An array is a group of variables that have the

    same name and data type and are related insome way

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

    Programmers use arrays to store related datain the internal memory of the computer

  • 7/27/2019 MELJUN CORTES Array Discussion

    3/24

    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.

  • 7/27/2019 MELJUN CORTES Array Discussion

    4/24

    Syntax Elementsin Array Declaration

    Description

    Array name The name youll use to represent your array in

    the program. In general,array names follow the same rules as variablenames.

    Data type The type of data youll store in the array. In mostcases, all thevariables in an array are the same type.

    Number of dimensions

    The number of dimensions that your array willcontain. 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 willcontain. The elements inyour array correspond directly to the array index.The first array indexis always 0 (zero).

  • 7/27/2019 MELJUN CORTES Array Discussion

    5/24

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

    Dim Employees(9) As String

    Public Employees(9) As String

    Dim Employees(0 To 9) As String

  • 7/27/2019 MELJUN CORTES Array Discussion

    6/24

    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

  • 7/27/2019 MELJUN CORTES Array Discussion

    7/24

    To refer to an element of an array, you use thearray name and an arrayindex enclosed inparentheses.

    Employees(5) = "Leslie"

  • 7/27/2019 MELJUN CORTES Array Discussion

    8/24

    To refer to an element of an array, you usethe array name and an array index enclosedin parentheses.

    Scoreboard(0, 2) = 4

  • 7/27/2019 MELJUN CORTES Array Discussion

    9/24

    To create an array in this manner, you usewhat is called an array literalAn array literal consists of a list of comma-separated values that are enclosed in braces({}).

  • 7/27/2019 MELJUN CORTES Array Discussion

    10/24

    A subscript must reference a valid element of the array. lf a list contains 10 names itwouldn't make sense to ask: What is the15th name on the list? Or What is the 21/2th

    name on the list? Visual Basic roundsfractional subscripts and throws an exceptionfor 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.

  • 7/27/2019 MELJUN CORTES Array Discussion

    11/24

  • 7/27/2019 MELJUN CORTES Array Discussion

    12/24

  • 7/27/2019 MELJUN CORTES Array Discussion

    13/24

  • 7/27/2019 MELJUN CORTES Array Discussion

    14/24

  • 7/27/2019 MELJUN CORTES Array Discussion

    15/24

  • 7/27/2019 MELJUN CORTES Array Discussion

    16/24

  • 7/27/2019 MELJUN CORTES Array Discussion

    17/24

  • 7/27/2019 MELJUN CORTES Array Discussion

    18/24

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

    LBound( ArrayName)UBound( ArrayName)

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

  • 7/27/2019 MELJUN CORTES Array Discussion

    19/24

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

  • 7/27/2019 MELJUN CORTES Array Discussion

    20/24

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

  • 7/27/2019 MELJUN CORTES Array Discussion

    21/24

    ReDim statement specifies the size of a dynamic array at run timeIf you redimension an array that already hasdata in it, all the existing data is irretrievablylost

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

  • 7/27/2019 MELJUN CORTES Array Discussion

    22/24

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

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

  • 7/27/2019 MELJUN CORTES Array Discussion

    23/24

    To use an array to store items that arerelated but have different data types.One solution is to use two parallel one-

    dimensional arrays: a String array tostore the IDs and an Integer array tostore the salaries.Parallel arrays are two or more arrayswhose elements are related by theirposition in the arrays.

  • 7/27/2019 MELJUN CORTES Array Discussion

    24/24