Autor Beitrag
buSC
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 39



BeitragVerfasst: Mo 22.02.10 14:56 
hallo
angenommen:
arr:array of integer;
jetzt will ich array mit nummern [ 2 7 9 10]
einfuellen ,gibts es ein schnellere schreibweise
als folgendes ?
arr[0]:=2;arr[1]:=7;arr[2]:=9;arr[3]:=10;
Achtung:mein array ist dynamic
Danke im Voraus


Zuletzt bearbeitet von buSC am Di 23.02.10 08:03, insgesamt 2-mal bearbeitet
Niko S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 566
Erhaltene Danke: 10

Win 7, Ubuntu
Lazarus, Turbo Delphi, Delphu 7 PE
BeitragVerfasst: Mo 22.02.10 17:07 
Öhm, mir ist keine Möglichkeit bekannt, aber ich hab mal was gebastelt.
Performant ist es sicherlich nicht, aber vielleicht hilft es dir ja.
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
procedure TForm1.FillArrayWithInt(var Arr: Array of Integer; Text: String);
var
  id, pos: integer;
begin
  id := 0;
  pos := 0;
  Repeat
    Arr[id] := StrToInt(Copy(Text,pos+1,PosEx(';',Text,pos+1)-pos-1));
    pos := PosEx(';',Text,pos+1);
    inc(id);
  Until (pos = 0or (pos = length(text));
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  i: Integer;
begin
  setlength(arr,10);
  FillArrayWithInt(Arr,'4;3;35;34;22;93;34;18;');
  for i := 0 to 7 do
  begin
    Memo1.lines.add(IntToStr(Arr[i]));
  end;
end;

Ich habs grad eben mal zusammengebastelt, gibt 100% noch ne möglichkeit, das noch Performanter zu machen.

PS: das ganze ist ca 0,0009761 Millisekunden langsamer, als das Manuelle eingeben (arr[0] := ... u.s.w.)
Der Unterschied bei 100 000 mal eine Array mit diesen Werten füllen, lag bei knapp 98 Millisekunden.

Achja und du musst die Unit "StrUtils" einbinden.
Narses
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Administrator
Beiträge: 10183
Erhaltene Danke: 1256

W10ent
TP3 .. D7pro .. D10.2CE
BeitragVerfasst: Mo 22.02.10 17:58 
Moin!

Bitte ändere den Titel des Topics, da er wenig über das eigentlich Thema verrät. Hier der entsprechende Absatz aus den Richtlinien:
1.2 Beiträge:
Bitte formuliere den Betreff Deiner Beiträge so, dass andere Mitglieder anhand dieser bereits das eigentliche Thema festmachen können. Beiträge wie etwa "Eine Anfängerfrage" oder "Weiß jemand, wie das geht?" lassen den Leser im Unklaren darüber, was das Thema der Diskussion ist.[...]
Einfach oben bei Deinem ersten Beitrag auf user defined image oder user defined image klicken und den Titel ändern. Danke Dir!

cu
Narses

_________________
There are 10 types of people - those who understand binary and those who don´t.
Hidden
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 2242
Erhaltene Danke: 55

Win10
VS Code, Delphi 2010 Prof.
BeitragVerfasst: Mo 22.02.10 18:06 
Hi :)

Warum kein Konstantes Array deklarieren, und die Werte rüberkopieren? Dürfte immernoch schneller sein :lupe:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
type
  TForm1 = class(TForm)
  {..}
  private
    FArr3: Array of String;
    {..}
  end;

procedure TForm1.InitDynamicArray(Arr: Array of Integer, Arr2: Array of TPoint);
var
  i: Integer;
const
  ArrValues1 = (013467910);
  ArrValues2 = ((02), (46), (810));
  ArrValues3 = ('1''2''6''24''120''720''5040''40320');
begin
  for i := 0 to Length(ArrValues1) - 1 do Arr[i] := ArrValues1[i];
  {..}
end;


lg,

_________________
Centaur spears can block many spells, but no one tries to block if they see that the spell is a certain shade of green. For this purpose it is useful to know some green stunning hexes. (HPMoR)
Xion
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
EE-Maler
Beiträge: 1952
Erhaltene Danke: 128

Windows XP
Delphi (2005, SmartInspect), SQL, Lua, Java (Eclipse), C++ (Visual Studio 2010, Qt Creator), Python (Blender), Prolog (SWIProlog), Haskell (ghci)
BeitragVerfasst: Mo 22.02.10 20:32 
Ich hab mal user profile iconNiko S. Beispiel umgebaut...

ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
procedure TForm1.FillArrayWithInt(var Arr: Array of Integer; Values: array of integer);
var
  A: integer;
begin
  for A:= 0 to High(Values) do
    Arr[A]:=Values[A];
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  i: Integer;
begin
  setlength(arr,10);
  FillArrayWithInt(Arr,[4,3,35,34,22,93,34,18]);
  for i := 0 to High(Arr) do
  begin
    Memo1.lines.add(IntToStr(Arr[i]));
  end;
end;

_________________
a broken heart is like a broken window - it'll never heal
In einem gut regierten Land ist Armut eine Schande, in einem schlecht regierten Reichtum. (Konfuzius)


Zuletzt bearbeitet von Xion am Mo 22.02.10 21:24, insgesamt 1-mal bearbeitet
Niko S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 566
Erhaltene Danke: 10

Win 7, Ubuntu
Lazarus, Turbo Delphi, Delphu 7 PE
BeitragVerfasst: Mo 22.02.10 20:46 
Ich wusste dass es doch noch anders geht!
Nun, die Lösung ist wohl die beste ;) Ich habs wenigstens versucht.
Marc.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1876
Erhaltene Danke: 129

Win 8.1, Xubuntu 15.10

BeitragVerfasst: Mo 22.02.10 21:25 
user profile iconXion: Das erste Setzen der Arraylänge in Zeile kann man sich sparen. :P
(Wurde bereits wegeditiert)

Es gehört sicherlich nicht zum guten Stil, aber müsste folgender Codeschnippsel nicht auch funktionieren (und das deutlich flotter) ? :gruebel:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
procedure FillArrayWithInt(var Arr: Array of Integer; Values: Array of Integer);
begin
   SetLength(Arr, Length(Values));
   CopyMemory(@Arr, @Values, Length(Values) * SizeOf(Integer));
end;

Hab gerade kein Delphi zur Hand...

Edit:
CopyMemory übergibt die Parameter weiter an die Prozedur Move(), die alles andere als flott aussieht - mal ganz abgesehen von der Pointerfrickelei. Ich denke damit kann man meinen Vorschlag dann begraben. ;)
ausblenden Delphi-Quelltext
1:
2:
3:
4:
procedure CopyMemory(Destination: Pointer; Source: Pointer; Length: DWORD);
begin
  Move(Source^, Destination^, Length);
end;


Zuletzt bearbeitet von Marc. am Mo 22.02.10 21:43, insgesamt 3-mal bearbeitet
Xion
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
EE-Maler
Beiträge: 1952
Erhaltene Danke: 128

Windows XP
Delphi (2005, SmartInspect), SQL, Lua, Java (Eclipse), C++ (Visual Studio 2010, Qt Creator), Python (Blender), Prolog (SWIProlog), Haskell (ghci)
BeitragVerfasst: Mo 22.02.10 21:31 
user profile iconMarc. hat folgendes geschrieben Zum zitierten Posting springen:
user profile iconXion: Das erste Setzen der Arraylänge in Zeile kann man sich sparen. :P

Ja, habs schon gesehen, das geht leider garnicht dass array in der Procedure zu ändern

Wie würde das denn aussehen wenn ich die Funktion auf den Typ Variant umstelle. Dann funktioniert das nicht, weil ich kein array of int zuweisen kann.


ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
procedure FillArrayWithInt(var Arr: Array of Variant; Values: array of Variant);
var
  A: integer;
begin
  for A:= 0 to High(Values) do
    Arr[A]:=Values[A];
end;

procedure TForm1.FormCreate(Sender: TObject);
var S: array of Variant;  
  arr: array of integer;
begin
  setlength(arr,10);
  setlength(S,10);
  FillArrayWithInt(Arr,[4,3,35,34,22,93,34,18]);  //=>[Fehler] Unit1.pas(79): E2010 Inkompatible Typen: 'Array' und 'dynamic array'
  FillArrayWithInt(S,[4,3,35,34,22,93,34,18]); //geht
end;

_________________
a broken heart is like a broken window - it'll never heal
In einem gut regierten Land ist Armut eine Schande, in einem schlecht regierten Reichtum. (Konfuzius)
buSC Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 39



BeitragVerfasst: Di 23.02.10 08:10 
Erstmal dank an dieses gute Forum
und auch danke an Xion,Marc.,Niko S.,Hidden,Narses
,die sich bemueht haben,mich zu antworten.
:)