Reference resources :http://www.cnblogs.com/huangjacky/archive/2009/12/21/1628833.html

An array is a collection of data with the same characteristics , That is, the type of each element must be the same , Of course, in other languages with weak grammar , The elements of an array can be various , Example

var
A : Array[0..2] of Integer;
begin
A[0] := 1;
a[1] := 1.0; // It's wrong here , Because every element has to be Integer type
end;

Delphi The classification of arrays in

1. Fixed length and indefinite length

Fixed length array

A fixed length array is defined at the time of declaration , The back can't be changed , And in fixed length arrays , The starting sequence number does not have to be from 0 Start , You can make it yourself , for example

var
A : Array[2..3] of Integer;
begin
A[2] := 1;
SetLength(A,3); // There will be mistakes here , Because fixed length arrays can't be reallocated
end;

We can see from the above that the starting number is 2, But the step size is 1, It can't be changed . Why do we see a lot of arrays start with 0 Well ? It's just a habit .

Let's look at 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];
//B[H] and C[1] They all make mistakes
end;

As can be seen from the above example Any ordinal type can be used as the ordinal number of an array , But when we use it, the ordinal number must be the declared ordinal type , That's why the above code comments only write two kinds of errors

Indefinite length arrays

Indefinite length arrays : The dynamic array , That is to say The length of the statement was not stated , It must be declared before use , Length can be redistributed , The serial number must be from 0 Start , Example

var
A : Array of Integer; // A possible definition of a fixed length array is A :Array[0..10] of Integer
begin
SetLength(A, 3); // Array has 3 Elements
A[0] := 1;
A[1] := 2;
A[2] := 3;
//A[3] No, , Because only 3 Elements SetLength(A, 4); // If it gets longer , Add the following elements directly
A[3] := 4; // Now add a fourth element , And the first three elements are still those three SetLength(A, 3); // If the length gets shorter , The excess will be removed
// Now? A[3] period
end;

occasionally , You have to set the length first , In assignment , so much trouble , Now let's introduce a one-stop trick

type
TA = Array of Integer;
var
A : TA;
begin
A := TA.Create(1, 2, 3);
// Please don't do this in Delphi 7 The use of // After using the above method ,A[0] :=1, A[1] := 2, A[2]:=3
end;

2. One dimension and many dimensions

All the previous examples , We're talking about one-dimensional arrays , To make a matrix ( Multidimensional arrays ) What do I do ?

var
A : Array[0..2, 0..2] of Integer;
B : Array[0..2] of Array[0..2] of Integer;
begin
A[0, 0] := 1;
B[0, 0] := 1;
end;

Both methods are possible , The following introduces the indefinite length array in the two-dimensional array

var
B : Array of Array of Integer
begin
// Set up a 3*3 Matrix
SetLength(B, 3, 3); // If you need to implement a dentate array , You have to do it like this
SetLength(B, 3);
SetLength(B[0], 1);
SetLength(B[1], 2);
SetLength(B[2], 3);
end;

Next, I'll introduce some common functions about arrays

1. Copy function

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:2
end;

This effect is what we want , There seems to be nothing to say , But if it's 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:2
// Obviously changed B[0] After the value of , Yes A[0] It also has an impact , therefore B and A There's a connection between
end;

What now? ?A and B Linked to an address , Now we can actually use Copy function , We can solve this problem

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 assigned a value
B := Copy(A, 0, 2); // Selective replication
B[0] := 2;
ShowMessageFmt('A0:%D,B0:%D', [A[0], B[0]]); // A0:1,B0:2
end;

2. The serial number is related to

function Low() and High() trustworthy , But here's the thing , The type they return is the type of the ordinal number of our array , Not all of them are Integer, As in the previous example THuangJacky

var
A : Array of array of string;
I, J : Integer;
begin
SetLength(A, 10);
for I := Low(A) to High(A) do
begin
SetLength(A[I], I);
for J := Low(A[I]) to High(A[I]) do
A[I, J] := IntToStr(I) + ',' + IntToStr(J) + ' ';
end
end;
end.

3. The length of the array

Length() Function returns Integer type

var
A : Array of Integer;
begin
SetLength(A, 2);
Length(A);
end.

From the above example, we can see that : A constant length array variable is a variable , So you can use it directly := To assign a value , And a dynamic array variable is a pointer , If used := To assign a value , The two variables are linked together

var
A :Array[0..2] of Integer;
B :Array of Integer;
begin
ShowMessageFmt('A:%8x, A[0]:%8p', [Integer(@A), @A[0]]); // equally , From the address, this array control is on the stack
SetLength(B, 3);
ShowMessageFmt('B:%8p, B[0]:%8p', [B, @B[0]]); // equally , This data space is on the heap
end

We see A You have to get an address to get in touch with A[0] It's the same as the address , So in other words A Namely A[0];

and B Directly with B[0] It's the same as taking the address , in other words B Namely B[0] The address of

The distribution of arrays in memory : Continuously distributed , The interval is the size of each element

var
A: Array[0..2] of Integer;
B: Array of Integer;
begin
A[1] := 123;
// from A That is to say A[0] Go down the top of your address 4 The first is directly A[1]
ShowMessageFmt('A[1]:%D, Direct value :%D',[A[1], PInteger(Integer(@A)+4)^]);
// equally , All are 123
SetLength(B, 3);
B[2] := 88;
// from B Go down 8 Bytes is B[2]
ShowMessageFmt('B[2]:%D, Direct value :%D',[B[2],PInteger(Integer(B)+8)^]);
end;

But the structure of dynamic array is very similar to that of character

The offset -8 -4 0~Length* Elements
Content 32 Number of bit references Element number The actual content

Delphi Array of more related articles

  1. delphi How to initialize an array

    https://wenda.so.com/q/1535561587217078delphi How to initialize an array rosegirl09112 Class classification : Others are browsed 44 Time 2018.07.01 The prosecution is satisfied with the answer csx330 ...

  2. delphi Ingenious use of array type and array pointer

    { This example uses the access structure , Slowly introduced some usage of array type and pointer ; The test content and results of six small examples are the same . ---------------------------------------------- ...

  3. delphi An array type

    An array type An array type defines a sequence of elements of a specified type , You can access the elements in the array by filling in the subscript value in the square brackets . When defining an array , Square brackets are also used to specify possible subscript values . for example , In the following code, there is a definition with 24 Array of integers :type     ...

  4. delphi The rookie of array

    An array is a list of data of the same type that can be referenced by an index . According to the way the storage space is obtained ,Delphi There are two types of arrays supported , Static array and dynamic array . A static array is an array type whose size has been determined at the time of declaration , The dynamic array means that its size is in the declaration ...

  5. delphi Use of arrays

    delphi The array in is just like string Use similar , The array is divided into : Dynamic arrays and static arrays It can also be divided into : Array ( One dimensional array ). Two dimensional array . Three dimensional array ... Static array : Fixed length , Add... When content needs to be defined . The dynamic array : Hence the name Si ...

  6. Delphi - Array Detailed explanation

    Technical communication ,DH Explain . First we need to know what an array is ? An array is a collection of data with the same characteristics , That is, the type of each element must be the same , Of course, in other languages with weak grammar , The elements of an array can be various . Example : ? 1 2 3 4 ...

  7. delphi Array as a parameter

    In a function, if the number of arrays is indefinite , You can use open array parameters Arguments can accept static and dynamic arrays procedure p1(a:array of Byte); begin ShowMessage( IntToHex( ...

  8. DELPHI Array , The pointer , Examples of string conversion

    About arrays , The pointer , Examples of string conversion var   aa:   array [0..5] of Char;   bb:Pointer;   cc:string;   dd:PChar; procedure ...

  9. Delphi - Arrays and structures

    Technical communication ,DH Explain . I remember saying a long time ago , Arrays and structs are actually the same in memory , They're all continuously distributed . for example : ? 1 2 3 4 TMyStruct = record   A,B,C:Integer; en ...

Random recommendation

  1. pt-ioprofile

    pt-ioprofile It's used to observe a particular process IO The information of . The script uses shell Written , There are two functions : pt-ioprofile does two things: ) ) is not performe ...

  2. osharp3 Adjustment of operation result category after introducing transaction

    /// <summary> /// Enumeration representing the results of business operations , /// There are only two states that affect the business operation unit , /// success , unchanged : The operation will continue , Business will continue /// Failure : Will lead to The operation is in progress ...

  3. MySQL Installation and master-slave configuration

    System environment :CentOS release 6.5 (Final)( Minimize installation ) MySQL edition :mysql-5.6.12 Cmake edition :cmake-2.8.4 explain : install mysql Install first cmake( primary ...

  4. IP How the address is stored in the database

    Most companies need to go through DBA Review , Sometimes you see storage IP The address is varchar(15), It's all traditional , This method takes up 15 Bytes , So is there a way to save space ? There must be , That's using int ...

  5. Android Customize View ( 3、 ... and ) The circle alternates Wait for the effect

    Reprint please indicate the source :http://blog.csdn.net/lmj623565791/article/details/24500107 A friend has such a need today ( The figure below ), I think that custom View It's still very ...

  6. window Next mongodb Fast installation

    Download address https://www.mongodb.org/dl/win32/x86_64-2008plus-ssl Create folders and files # Database path dbpath=G:\mongodb3.4.12\da ...

  7. php Use root User start

    In general , Definitely not recommended root User start php Of But in some server management you want to use WEB To control the operation in a different way , Then you have to use root Users have the right to operate 1. Modify the configuration file php-fpm.conf Start user of ...

  8. remember Weblogic Deploy BUG(websocket)

    Will contain websocket Of SSM Project deployment Weblogic above , encounter websocket An error is as follows java.lang.ClassCastException: org.springframework.se ...

  9. c++ Interview questions ( Network class )

    1, If the server terminates alone , What happens when the client continues to send data ? https://blog.csdn.net/Nick_666/article/details/78342442 https://www.zhih ...

  10. [WC2008] Tour plan ( Steiner tree )

    [Luogu4294] Answer key : Steiner tree \(dp[i][j]\) Said to \(i\) Node number is the root , The current state is \(j\)( And \(i\) The connecting point is \(1\)) Danggen \(i\) The state transfer equation without change is : \( ...