About Arrays (original)

Source: Internet
Author: User
Tags arrays definition
Array | Original A defines an array

There are two ways: Dim and ReDim.

Dim defines an array of fixed numbers, data types, and ReDim, which can define different types of data or define numbers that are not fixed. Compare the following few examples. All legal Examples:

Dim MyArray (5,2)
Redim MyArray (5,2)

The former error and the latter legal example:

n=10 n=10

Dim myarray (N)
Redim MyArray (n,2)
Alternatively, Redim can also define an array of undetermined types, such as: Redim MyArray (10)

 

Number of B arrays

The subscript specified when the array is defined with Dim or ReDim represents the maximum subscript allowed when accessing the array, but not the number of that array. In fact, the number of one-dimensional arrays is always equal to (the maximum subscript + 1), accessed by the subscript starting from 0.

For example, the Dim myarray (5) defines an array element of 6, respectively: MyArray (0), myarray (1), myarray (2), MyArray (3), MyArray (4), MyArray (5).

Another example: Redim thisarray (2,5) actually defines a two-dimensional array of (2+1) * (5+1) =1 8.

So, then, is it an immutable definition of an array of only one element? The answer is: No.

As mentioned earlier, the array defined by the Redim thisarray (1) actually has (1+1) arrays of elements, but is similar to: Redim thisarray (0) syntax, error. Therefore, you cannot define an array that has only one array element. In fact, the above is only the default status. In fact, the definition of an array can be defined by defining the beginning and end of the index to define the number of arrays or even subscript the beginning and end numbering. For example: Redim thisarray (1980 to1990) defines an array containing 11 elements, subscript 1980 through 1990.

 

C About UBound functions

UBound returns the largest subscript of a one-dimensional array, not the number of elements. For example: Dim myarray (5), then UBound (myarray) returns a value of 5 instead of 6. UBound can also be applied to two-dimensional arrays. When applied to a two-dimensional array, it returns the maximum value of the first subscript.

For example: Dim myarray (6,3),

Then UBound (myarray) returns a value of 6 instead of 7, not 18 (6*3=18).

To return the maximum value for the second subscript, use: UBOUND (myarray,2).

Corresponding to the UBound is another function: LBOUND, which returns the lowest subscript of the array. Similar to UBound, LBOUND (myarray,2) returns the minimum value of the second subscript of the array myarray. So, to be exact, the number of elements in a one-dimensional array myarray is: UBOUND (myarray)-lbound (myarray) +1, and the number of elements in the two-dimensional array is:

(UBOUND (myarray)-lbound (myarray) +1) * (UBOUND (myarray,2)-lbound (myarray,2) +1)

multidimensional arrays, and so on.

Oh, hope to join the essence.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.