Autor Beitrag
mk2
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 72

WIN 2000
D4 Prof
BeitragVerfasst: Di 22.10.02 14:28 
hallo,

ich arbeite sehr viel mit dynamischen arrays in letzter zeit. Das einzige was mich stört, ist das kopieren - ich mach das zur zeit so (a1 nach a2 kopieren):

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
var i:integer;
     a1,a2:array of integer;
begin
 setlength(a2,length(a1));
 for i:=0 to high(a1) do a2[i]:=a1[i]; 
end;


da mir das immer ein bißchen viel ist wollt ich mir ne funktion zum array kopieren schreiben die den code oben ausführt. Also im prinzip so :

ausblenden Quelltext
1:
function copyarray(a1:array of integer):array of integer;					

funktioniert aber leider nicht. eine funktion kann anscheinend kein dyn. array zurückgeben.

hat jemand vielleicht eine idee, wie man das mit dem kopieren trotzdem bißchen einfacher machen kann ? Gibts schon eine mitgelieferte funktion, die das erledigt ?

mfg mk2
O'rallY
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 563



BeitragVerfasst: Di 22.10.02 14:36 
Das Problem hatte ich kürzlich auch (drei Posts weiter unten) und die Lösung ist ziemlich simpel.
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
type
   TAoInteger: array of integer; //eigener Datentyp

...
var
   a1, a2: TAoInteger;
...
begin
   a2 := a1; {stinknormale Zuweisung, wobei SetLength natürlich nicht benötigt wird}
...

_________________
.oO'rallY
Linux is like a tipi: No gates, no windows and a gnu-eating apache inside...
Tante
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 41



BeitragVerfasst: Di 22.10.02 14:39 
Hallo, wie wäre es, wenn Du beide Arrays als var-Parameter angibst?
Also folgende Prozedur:
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
procedure copyarray(var a1,a2:array of integer);
var i:integer; 
  begin 
  setlength(a2,length(a1)); 
  for i:=0 to high(a1) do a2[i]:=a1[i]; 
  end;

Ist nur eine Idee, nicht getestet. :D
AndyB
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 1173
Erhaltene Danke: 14


RAD Studio XE2
BeitragVerfasst: Di 22.10.02 14:50 
Und um es noch einfacher zu machen. Wie wäre es, wenn man die Copy-Funktion von Delphi nutzt?

ausblenden Quelltext
1:
2:
3:
4:
5:
6:
var MyArray1, MyArray2: array of integer;
begin
  SetLength(MyArray1, 10);
  MyArray[0] := ...;
  MyArray2 := Copy(MyArray1);
end;

Zitat:
Aus der Delphi-Hilfe:
Note: When S is a dynamic array, you can omit the Index and Count parameters and Copy copies the entire array.

_________________
Ist Zeit wirklich Geld?
mk2 Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 72

WIN 2000
D4 Prof
BeitragVerfasst: Di 22.10.02 15:00 
danke euch für die schnelle hilfe.

das mit der copy funktion ist natürlich das allereinfachste. genau sowas hab ich gesucht. hätt ich ja auch mal selber drauf kommen können unter copy nachzugucken. :oops:

mfg mk2
aogwaba
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 109



BeitragVerfasst: Di 22.10.02 19:07 
@O'rallY

wenn du das so machst:
ausblenden Quelltext
1:
 a1:=a2;					

hast du zwei Referenzen auf das selbe Array!

Kopiert wird da nix.

cu
waba
O'rallY
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 563



BeitragVerfasst: Mi 23.10.02 13:26 
Hmm, klingt vernünftig. Auf jedenfall hat mein Lösungsweg mir helfen könne, vielleicht auch in einem anderen Kontext :mrgeen:.

_________________
.oO'rallY
Linux is like a tipi: No gates, no windows and a gnu-eating apache inside...
TheLaw
Hält's aus hier
Beiträge: 4



BeitragVerfasst: Do 24.10.02 21:27 
Hallo,

Also die dynamischen Arrays bei Delphi Arbeiten mit CopyOnWrite- Semantik.

Beispiel:
ausblenden Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
var
  A, B: array of Byte;
begin
  SetLength(A, 10);
  B := A; // Referenzzähler von A wird erhöht 
              // B und A zeigen auf das gleiche Array
  B[0] := 2 // Für Array B wird Speicher reserviert 
                // und Array A wird in B Kopiert und der
                // erste Index auf [b]2[/b] gesetzt
end;


Ich hoffe es ist jetzt "klärer"

mfg Th3Law