Delphi-array details

Source: Internet
Author: User

Technical Exchange, DH explanation.

First, we need to know what an array is?An array is a collection of data with the same features, that is, the type of each element must be the same.Of course, in some other languages with weak syntax, the array elements can be odd.
Example:

 
VaR A: array [0 .. 2] of integer; begin a [0]: = 1; A [1]: = 1.0; // here is an error, because each element must be of the integer type end;

 

Array classification in Delphi:
1Fixed Length and Variable Length.
Fixed Length array: that isThe length is determined during the declaration, and cannot be changed later.In a fixed-length array, the Starting sequence number does not have to start from 0. You can set it by yourself. For example:

 
VaR A: array [2 .. 3] of integer; begin a [2]: = 1; setlength (A, 3); // an error occurs here, and the fixed length array cannot be assigned end;

 

From the above we can see that the Starting sequence number is 2, but the step size is 1, it cannot be changed. Why do we see that the Starting sequence numbers of many arrays are 0? Habits. everyone is used to booting in the toilet, and you are used to booting on the square, so everyone will say that you are uncivilized. however, it would be disgusting to say that everyone had been on the square in the first place.
Here is a special usage:

 
Type thuangjacky = (hja, hjb, HJC); const // usage 1 B: array [0 .. 2] of string = ('A', 'B', 'C'); // usage 2 C: array [thuangjacky] of string = ('A ', 'B', 'C'); var H: thuangjacky; s: string; begin S: = B [ord (h)]; S: = C [H]; // both B [H] and C [1] have an end error;

 

Usage 1 and usage 2 do you think it is better to use it?
As shown in the preceding exampleAs long as it is of the ordinal type, it can be used as the serial number of the array.But when we use the sequence number, it must be the declared sequence type, so the aboveCodeTwo errors will be written in the annotations.
Variable Length array: dynamic array, that isThe length is not stated during declaration. It must be declared before use and can be redistributed. The sequence number must start from 0.. Let's look at a simple example.

VaR A: array of integer; begin setlength (A, 3); // an array contains three elements a [0]: = 1; A [1]: = 2; A [2]: = 3; // If a [3] does not have it, how many times have you counted it? I recommend you go to the street entrance elementary school to check the setlength (A, 4); // If the length is longer, directly Add the following element a [3]: = 4; // now it is available. setlength (A, 3); // If the length is shorter, the excess part will be removed. // A [3] has no end;

 

Sometimes we need to set the length first and then assign a value, isn't it very troublesome? I don't feel it in one breath. Okay, let's say:

 
Type TA = array of integer; var A: Ta; begin A: = TA. create (1, 2, 3); // do not use this action on D7 // A [0]: = 1, a [1]: = 2, A [2]: = 3end;

 

21D and multidimensional.
In all the previous examples, we only talked about one-dimensional arrays. What if we want to create a matrix (multi-dimensional array?

 
VaR A: array [0 .. 2, 0 .. 2] of integer; B: array [0 .. 2] of array [0 .. 2] of integer; begin a [0, 0]: = 1; A [0] [0]: = 1; end;

Either method works.

 
VaR B: array of integer; begin setlength (B, 3, 3); // 3*3 matrix // setlength (B, 3); setlength (B [0], 1); // * setlength (B [1], 2); // ** setlength (B [2], 3 ); // *** end;

 

Next, let's talk about several common functions in Arrays:
FirstCopy an array

VaR A, B: array [0 .. 1] of integer; begin a [0]: = 1; A [1]: = 2; B: = A; B [0]: = 2; showmessagefmt ('a0: % d, B0: % d', [A [0], B [0]); // A0: 1, B0: 2end;

This effect is what we want. It seems that there is nothing to say. What if it is a dynamic array?

 
VaR A, B: array of integer; begin setlength (A, 2); setlength (B, 2); A [0]: = 1; A [1]: = 2; b: = A; B [0]: = 2; showmessagefmt ('a0: % d, B0: % d', [A [0], B [0]); // A0: 2, B0: 2end;

What should I do now? A and B are associated with an address. Now we can use the copy function, which is the function used to copy the string:

VaR A, B: array of integer; begin setlength (A, 2); setlength (B, 2); A [0]: = 1; A [1]: = 2; b: = copy (a); // the entire array is copied. B: = copy (A, 0, 2); // You can selectively copy B [0]: = 2; showmessagefmt ('a0: % d, B0: % d', [A [0], B [0]); // A0: 1, B0: 2end;

SecondSequence Number
The functions low () and high () are trustworthy, but we need to note that they return the type of the serial number of our array, not all integer, as shown in the preceding example.

VaR A: array of string; I, J: integer; begin setlength (A, 10); for I: = low (A) to high () do begin setlength (A [I], I); For J: = low (A [I]) to high (A [I]) Do A [I, j]: = inttostr (I) + ',' + inttostr (j) + ''; end;

ThirdArray Length
The Return Value of the length () function must be an integer, because the number is not an integer. Is there another half?

 
VaR A: array of integer; begin setlength (A, 2); length (a); // returns 2end;
 

I will not talk about the last question:
What can we see from the copied example above?
The fixed-length array variable is a variable, so you can directly use = to assign values, while the dynamic array variable is a pointer. If you use: = to assign values to two variables, they will be associated..

 
VaR A: array [0 .. 2] of integer; B: array of integer; begin showmessagefmt ('a: % 8x, a [0]: % 8p', [INTEGER (@ ), @ A [0]); // The same, from the address, the array space on the stack is setlength (B, 3); showmessagefmt ('B: % 8 p, B [0]: % 8p ', [B, @ B [0]); // same, the data space ends on the heap;

We can see that a needs the same address as a [0], so a is a [0].
B is the same as B [0], that is, B is the address of B [0.

Array Distribution in memory: continuous distribution. The interval is the size of each element.

VaR A: array [0 .. 2] of integer; B: array of integer; begin a [1]: = 123; // go down from the address a, that is, a [0], and go down to the address 4 directly, that is, a [1] showmessagefmt ('a [1]: % d. The value is directly set: % d', [A [1], pinteger (INTEGER (@ A) + 4) ^]); // All values are 123 setlength (B, 3 ); B [2]: = 88; // 8 bytes down from B is B [2] showmessagefmt ('B [2]: % d, direct value: % d ', [B [2], pinteger (INTEGER (B) + 8) ^]); // same, 88end;

However, the structure of the dynamic array is similar to that of the character:

Offset -8 -4 0 ~ Length * element size-1
Content 32-bit reference count Number of Elements Actual content

Well, I can only say so much. I only understand so much.

I am DH.

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.