Important Methods of Array with sample code

Posted on at


 

Clear - sets a range of elements in an array to 0, false or a null reference, depending on the type of the array elements

 

Copy - copies a range of elements from one array to another.

Syntax:

Arrayvar.COPY(source,destination,length)

Dim inta(2), intb(2), intz As Integer

        For intz = 0 To 2

            inta(intz) = InputBox("enter a number", "")

        Next

        inta.Copy('inta, intb, 3)

        For intz = 0 To 2

            ListBox1.Items.Add(intb(intz))

        Next

 

Getlength – gets the number of elements in a specified dimension of the array

Syntax:

Arrayvar.GETLENGTH(dimension)

Example

Dim inta(2) As Integer

Txtdisplay.text = inta.GetLength(0)

OUTPUT: 3

Note: 0 – one dim array

          1 – two dim array

 

Getlowerbound – gets the lower bound of the specified dimension:

Syntax: Arrayvar.getlowerbound(dimension)

Example

Dim inta(2), intb(2,2) As Integer

Txtdisplay.text = inta.GetLowerBound(0)

Output: 0

Txtdisplay.text = intb.GetLowerBound(0)

Output: 0

 

Getupperbound – gets the upper bound of the specified dimension

Syntax: Arrayvar.getupperbound(dimension)

Example

Dim inta(2), intb(2,4) As Integer

Txtdisplay.text = inta.GetupperBound(1)

Output: 2

Txtdisplay.text = intb.GetupperBound(1)

Output: 4

Txtdisplay.text = intb.GetupperBound(0)

Output: 2

 

 

Getvalue – gets the value of the specified element in the array

Syntax: Arrayvar.getvalue(array index)

Example

 Dim inta(2)

        inta(0) = 3

        inta(1) = 4

        inta(2) = 2

     TextBox1.Text = inta.GetValue(1)

Output: 4

TextBox1.Text = inta.GetValue(0)

Output: 3

 

Indexof – returns the index of the first occurrence of the value in one dimensional array

Syntax: Arrayvar.indexof(arrayvar, value)

Example

  Dim inta(4 )as integer       

inta(0) = 3

        inta(1) = 4

        inta(2) = 3

        inta(3) = 5

        inta(4) = 2

        TextBox1.Text = inta.IndexOf(inta, 5)

Output: 3

TextBox1.Text = inta.IndexOf(inta, 3)

Output: 0

Note: Output is the first instance of the value

 

Reverse – reverses the order of the elements in one dimensional array

Syntax: Arrayvar.reversef(arrayvar)

Example

  Dim inta(4), intz As Integer

        inta(0) = 3

        inta(1) = 4

        inta(2) = 3

        inta(3) = 5

        inta(4) = 2

        inta.Reverse(inta)

        For intz = 0 To 4

            txtdisplay.text &=   “   “ &(inta(intz))

        Next

Output:      2       5       3       4       3

 

Setvalue – sets the specified element in the current array to the specified value

Syntax: Arrayvar.setvalue(value,index)

Example

  Dim inta(4), intz As Integer

        inta(0) = 3

        inta(1) = 4

        inta(2) = 3

        inta(3) = 5

        inta(4) = 2

        inta.SetValue(6, 3)

        For intz = 0 To 4

            txtdisplay.text &= “     “& (inta(intz))

        NextOutput:         3       4       3       6       2

 

Sort – sorts the elements in one dimensional array

Syntax: Arrayvar.sort(arrayvar)

Example

  Dim inta(4), intz As Integer

        inta(0) = 3

        inta(1) = 1

        inta(2) = 3

        inta(3) = 5

        inta(4) = 2

        inta.Sort(inta)

        For intz = 0 To 4

           txtdisplay.text &=  “   “ & (inta(intz))

        Next

Output: 1   2       3       3       5

 

Isfixedsize -  a Boolean value indicating whether the array has a fixed size

Syntax: Arrayvar.isfixedsize(arrayvar)

Example

  Dim inta(4), intz As Integer

        inta(0) = 3

        inta(1) = 1

        inta(2) = 3

        inta(3) = 5

        inta(4) = 2

        Txtdisplay1.Text = inta.IsFixedSize

Output: True

 

Length – total number of elements in all of the dimension of an array

Syntax: Arrayvar.Length(arrayvar)

Example

  Dim inta(4), intz As Integer

        inta(0) = 3

        inta(1) = 1

        inta(2) = 3

        inta(3) = 5

        inta(4) = 2

        TextBox1.Text = inta.Length

Output: 5

 

 

Rank – the number of dimension in an array

Syntax: Arrayvar.rank(arrayvar)

Example

  Dim inta(4), intz, intb(2,3) As Integer

        inta(0) = 3

        inta(1) = 1

        inta(2) = 3

        inta(3) = 5

        inta(4) = 2

        Txtdisplay.Text = inta.rank

Output :     1

          txtdisplay.text = intb.rank

Output :     2

 

 



About the author

alyka

Happy Person

Subscribe 0
160