I understand the array types in Delphi

Source: Internet
Author: User

An array can have any data type that an Object Pascal owns, and an array is a simple collection of some numeric values.

varMyArray:Array[0..4] ofInteger;{declares an array consisting of 5 integer values}beginmyarray[0] := - $;{You can access each array element by using the operator []}myarray[1] := - -; myarray[2] :=0; myarray[3] := -; myarray[4] := $; myarray[0]: = myarray[1] + myarray[4];{myarray[0] is -100}End;

The distribution of its myarray in memory space requires 4 bytes per integer, so the entire array will account for 20 bytes of memory, as follows:

1. Multidimensional arrays

ConstCArray:Array[0..4] ofInteger = (- -, - -,0, -, $); {An array constant is also assigned an initial value at the time of declaration.}varMyArray:Array[0..2,0..4] ofInteger;{Declaration of a two-D array}Uarray:Array[Ten.. -] ofInteger;{An array of 11 elements that declares a nether 10 to an upper bound of 20}X:integer;begin                                    {two ways to access two-D arrays}X:= myarray[1][1] + myarray[2][1];{1. [X][y] Access}X:= myarray[1,1] + myarray[2,1];{2, [X, Y] Access}   {The following access is outside the array range and will prompt "Constant expression violates subrange bounds" error}X:= myarray[3,1] + myarray[0,5];{Note that these two elements are outside the scope of the array declaration}End;

Where MyArray is declared as a two-dimensional array, its distribution in memory is as follows:

2. Upper bound and lower bound

The upper bound (low) and lower bound (high) functions are often used for working with arrays. Such as:

varX, I, Lower, Upper:integer; MyArray:Array[Ten.. -] ofInteger;begin  {in this case, if the myarray array has been assigned an initial value}Lower:= Low (MyArray);{the value of lower is ten}Upper:= High (MyArray);{the value of upper is}X:=0; forI: = Lower toUpper Do  beginX:= X + myarray[i];{x will accumulate the sum of the array element values}  End;End;

Use the upper bound function to ensure that the array is accessed without crossing the bounds.

By the way, if we change to a two-dimensional array, how do the upper and lower bounds function???

varLower1, Upper1:integer;{used to store one-dimensional upper and lower bounds}Lower2, Upper2:integer;{used to store two-dimensional upper and lower bounds}MyArray:Array[Ten.. -,1..2] ofInteger;begin  {in this case, if the myarray array has been assigned an initial value}Lower1:= Low (MyArray);{the value of lower is ten}Upper1:= High (MyArray);{the value of upper is}ShowMessage ('the lower bound of the first dimension is'+ INTTOSTR (Lower1) +', the upper bound is'+IntToStr (Upper1)); Lower2:= Low (Myarray[lower1]);{get myarray[10] Nether}Upper2:= High (Myarray[lower1]);{get myarray[10] Upper bound}ShowMessage ('the lower bound of the second dimension is'+ INTTOSTR (Lower2) +', the upper bound is'+IntToStr (Upper2));End;

Two times the message box display interface is as follows:

3. Dynamic array

A dynamic array is an array of memory allocated at run time, and a dynamic array can become larger or smaller.

Declare a dynamic array, as long as you do not make the dimensions at the time of declaration, like this:

varSA:Array  of string;{One-dimensional dynamic array}begin  {using SetLength for spatial allocation of dynamic arrays, existing elements can be preserved}SetLength (SA,3); sa[0] :='Hello World'; {The dynamic array size is reassigned to 2 elements}SetLength (SA,2); ShowMessage (sa[0]);{display as ' Hello World ', stating that existing elements are preserved}End;

In the same way, a two-dimensional dynamic array can be created, as follows:

varSA:Array  of Array  of string;{Two-dimensional dynamic array}begin  {using SetLength for spatial allocation of dynamic arrays, existing elements can be preserved}SetLength (SA, -, -); sa[0][0] :='Hello World'; {The dynamic array size is reassigned to 2 elements}SetLength (SA,Ten,Ten); ShowMessage (sa[0][0]);{display as ' Hello World ', stating that existing elements are preserved}End;

Dynamic arrays can be used as normal arrays once they are created.

Dynamic arrays are usually based on a 0 base.

Dynamic arrays are self-managing for lifetimes and are not necessary to be freed after they have been used, and are freed automatically when they leave the scope. Of course, if you want to delete a dynamic array before leaving the scope (for example, it takes up too much memory and needs to be freed), use the following statement.

var Array of string;   begin    );  sa[0'HelloWorld';  SA:nil{ The value of nil is directly assigned to SA and it can be }end;

Replication problems for dynamic arrays

varA1, A2:Array  ofInteger;beginSetLength (A1,4); A2:=A1; a1[0] :=1; a2[0] := -; ShowMessage (IntToStr (a1[0]));{Display result of}  {Why is 26 displayed? Because A2: = A1 This assignment statement, and did not create a new array, just to assign a reference to the A1 array to A2, that is, A2 is just an alias of A1, pointing to the same thing, so any action on A2 will affect A1, If you want to create a new A2 array for full replication, you need to use the copy function, as follows:}A2:= Copy (A1);{A1, A2 is now a two independent array}a2[0] :=Ten; a1[0] := -; ShowMessage (IntToStr (a2[0]));{now the value of A2 is 10, which means it's completely independent.}A2:=Nil;{release the A2 .}A2:= Copy (A1,1,2);{starting with element 1, copy 2 elements to A2}End;

Http://www.cnblogs.com/pchmonster/archive/2011/12/15/2288738.html

I understand the array types in Delphi

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.