Autor Beitrag
Marco D.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: Sa 13.05.06 16:36 
Hallo,

ich will zwei Einträge eines Arrays vertauschen und habe dazu folgende Prozedur geschrieben:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
procedure TForm1.ExchangePositionsInArray(InputArray : TMyArray; Index1,Index2 : integer);
begin
  if (Index1<High(InputArray)) and (Index2<High(InputArray)) then
  begin
    //Das Array um 1 verlängern
    SetLength(InputArray,Length(InputArray)+1);
    //Den zweiten Eintrag ans Ende kopieren, um ihn dort 'zwischenzulagern'
    InputArray[High(InputArray)]:=InputArray[Index2];
    //Den zweiten Eintrag mit dem ersten überschreiben
    InputArray[Index2]:=InputArray[Index1];
    //Den ersten Eintrag mit dem letzten überschreiben, der identisch mit dem zweiten ist
    InputArray[Index1]:=InputArray[High(InputArray)];
    //Das Array wieder um eins verkürzen
    SetLength(InputArray,Length(InputArray)-1);
  end;
end;

Nur leider funktioniert das nicht, sprich, ese tut sich nichts.

_________________
Pascal keeps your hand tied. C gives you enough rope to hang yourself. C++ gives you enough rope to shoot yourself in the foot
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8548
Erhaltene Danke: 477

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Sa 13.05.06 16:41 
:autsch: Zweimal Setlength zum vertauschen? Dafür wird zweimal das komplette Array im Speicher hin und herkopiert!

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
procedure swap(aArray: TMyArray; index1:integer; Index2:integer)
var tmp: integer; //oder was auch immer in TMyArray drinsteckt
begin
  tmp := aArray[index1];
  aArray[index1] := aArray[index2];
  aArray[index2] := tmp;
end;


Das geht wesentlich schneller ;-)

_________________
We are, we were and will not be.
Marco D. Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2750

Windows Vista
Delphi 7, Delphi 2005 PE, PHP 4 + 5 (Notepad++), Java (Eclipse), XML, XML Schema, ABAP, ABAP OO
BeitragVerfasst: Sa 13.05.06 16:47 
Da hast du allerdings recht :zwinker: :zustimm:

_________________
Pascal keeps your hand tied. C gives you enough rope to hang yourself. C++ gives you enough rope to shoot yourself in the foot