3.5.2 Dynamic Array

Source: Internet
Author: User

Dynamic Array means that the subscript in the brackets is not given when the array size is declared.) When you want to use it, use the ReDim statement to re-specify the array size at any time. The advantage is that the storage space can be effectively used according to user needs. The dynamic array is defined as follows:
Dim array name () [As data type] 'declares an array with no defined size or its data type.
An undefined array cannot be used. You can use the ReDim statement to redefine the array size. The format is as follows:
ReDim [Preserve] array name (expression)
You can use ReDim to redefine the array size multiple times. However, if the ReDim statement does not include the Preserve keyword, the data stored before the array is cleared after the array size is redefined. If you want to retain the previous data, use the Preserve keyword.
For example:

Dim arr () As Integer 'defines the dynamic array arr
Dim n As Integer = 10
ReDim arr (n) 'redefines the array arr and defines its size as 11
For I = 0 To 10
Arr (I) = I
Next
ReDim Preserve arr (20) 're-defines the array arr, defines its size as 21, and retains
'Arr (0 )~ Value of arr (10)

Note: The subscript in the static array declaration can only be a constant, while the subscript in the dynamic array ReDim statement can be a constant or a variable with a fixed value. For example, the preceding arr (n ).
You can use ReDim multiple times to change the array size.

BibliographyPrevious sectionNext section

Related Article

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.