Home > Back-end >  How to swap between two arrays in Delphi?
How to swap between two arrays in Delphi?

Time:09-30

How to swap between two arrays in Delphi?

Just like the following function
 
Procedure swap (var I and j: integer);
Var
I, j, TMP: integer;
The begin
TMP:=I;
I:=j;
J:=TMP;
end;

CodePudding user response:

Set an intermediate variable or array, read cycles

CodePudding user response:

reference 1st floor PTVBS response:
set an intermediate variable or array, loop read

In addition to the circulation, do you have any other methods?
Because multiple exchange array and array elements is more,
Loop speed will be slow,

CodePudding user response:

Exchanging data with intermediate buffer,

Var
ArrList0: an array of byte [0.. 5];
ArrList1: an array of byte [0.. 5];
ArrTemp: an array of byte [0.. 5];

//memory replication
Fillchar (arrtemp, 6, 0);
Move (pbyte (@ arrlist0 [0]) ^ and pbyte (@ arrtemp [0]) ^, 6);
Move (pbyte (@ arrlist1 [0]) ^ and pbyte (@ arrlist0 [0]) ^, 6);
Move (pbyte (@ arrtemp [0]) ^ and pbyte (@ arrlist1 [0]) ^, 6);

CodePudding user response:

Procedure TForm1. Hmary (var mary1 mary2: an array of string);
Var
Mary: an array of string;
I: Integer;
The begin
SetLength (Mary, High (mary1));
For I:=0 to high do (mary1) - 1
The begin
Mary: [I]=mary1 [I];
Mary1 [I] :=mary2 [I];
Mary2 [I] :=Mary [I];
end;
end;
Swap process

CodePudding user response:

Fix it: for I:=0 to high (mary1) do

Hight (mary1) should not be minus 1

CodePudding user response:

Note that Delphi is a 4 gl, its intelligent degree is high, the same type of the array can direct assignment, the compiler generates code processing elements replication problem (copy may only be a pointer, if is a dynamic array), and does not require users to worry about,
Note:
Type
TByteArray10=array [0.. 9] of byte;
Var
A1, a2, a3: an array of byte [0.. 9];//this is the same type
A4: an array of byte [0.. 9].//
A5: an array of byte [0.. 9];//
A6: an array of byte [0.. 9];//is not the same type, but can be cast with TByteArray10

Can direct assignment exchange like this:
A3:=a1;
A1:=a2;
A2:=a3;
TByteArray10 (a6) :=TByteArray10 (a4);
TByteArray10 (a4) :=TByteArray10 (a5);
TByteArray10 (a5) :=TByteArray10 (a6);

CodePudding user response:

refer to 6th floor DelphiGuy response:
note that Delphi is a 4 gl, it is a high degree of intelligence, the same type of the array can direct assignment, the compiler generates code processing elements replication problem (copy may only be a pointer, if is a dynamic array), and does not require users to worry about,
Note:
Type
TByteArray10=array [0.. 9] of byte;
Var
A1, a2, a3: an array of byte [0.. 9];//this is the same type
A4: an array of byte [0.. 9].//
A5: an array of byte [0.. 9];//
A6: an array of byte [0.. 9];//is not the same type, but can be cast with TByteArray10

Can direct assignment exchange like this:
A3:=a1;
A1:=a2;
A2:=a3;
TByteArray10 (a6) :=TByteArray10 (a4);
TByteArray10 (a4) :=TByteArray10 (a5);
TByteArray10 (a5) :=TByteArray10 (a6);

Whether the dynamic array can also handle?
So I use an Error (Error) Unit1. Pas (131) : Invalid typecast
 
Type
TArrInt=array of integer;

Procedure Swap (var x, y: an array of integer);
Var
TMP: an array of integer;
The begin
TArrInt (TMP) :=TArrInt (x);
TArrInt: (x)=TArrInt (y);
TArrInt: (y)=TArrInt (TMP);

CodePudding user response:

Unable to process the incoming parameter

CodePudding user response:

 
Procedure Swap (x, y: an array of integer);
Type
TByteArray10=array of integer;
Var
A1, a2, a3: an array of integer;//this is the same type
A4: an array of integer;//
A5: an array of integer;//
A6: an array of integer;//is not the same type, but can be cast with TByteArray10
The begin
//can direct assignment exchange like this:
A3:=a1;
A1:=a2;
A2:=a3;
TByteArray10 (a6) :=TByteArray10 (a4);
TByteArray10 (a4) :=TByteArray10 (a5);
TByteArray10: (x)=TByteArray10 (a6);//this compilation times wrong: Invalid typecast
end;

CodePudding user response:

Procedure Swap (var x, y: an array of integer); The statement of x, y type is not a dynamic array, but the open array parameter, this type of compatible static array parameters, dynamic array, to [] array of temporary structure, so the x, y actual type is not necessarily the same,
It is ok to like this:
Type
TArrInt=array of integer;

Procedure Swap (var x, y: TArrInt);
Var
Z: TArrInt;
The begin
Z:=x;
X: y=;
Y:=z;
end;

CodePudding user response:

The
references to the tenth floor DelphiGuy response:
procedure Swap (var x, y: an array of integer); The statement of x, y type is not a dynamic array, but the open array parameter, this type of compatible static array parameters, dynamic array, to [] array of temporary structure, so the x, y actual type is not necessarily the same,
It is ok to like this:
Type
TArrInt=array of integer;

Procedure Swap (var x, y: TArrInt);
Var
Z: TArrInt;
The begin
Z:=x;
X: y=;
Y:=z;
end;


Why
So you can compile
 
Procedure TForm1. Btn4Click (Sender: TObject);
Type
TByteArray=an array of byte;
Var
A1: an array of byte;
A2: an array of byte;
A3: an array of byte;//a1, a2, a3 is not the same type, but can be cast with TByteArray10
The begin
TByteArray (a3) :=TByteArray (a1);
TByteArray (a1) :=TByteArray (a2);
TByteArray (a2) :=TByteArray (a3);
End


And that will not be able to compile
 
Procedure Swap2 (x, y: an array of integer);
Type
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related