Autor Beitrag
rizor
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 209

WIN XP
Delphi 2005 PE, Visual C++
BeitragVerfasst: So 08.10.06 14:15 
Hallo,

ist es möglich ein Array zu erzeugen, dass automatisch eine bestimmte Anzahl an Array-Teilen erzeugt?

In dieses Array soll eine variable Anzahl an Strings rein.

Es sit duch auch möglich 1..maxint zu gehen, oder?
Dann werden aber zu viele Speicherplätze reserviert.
Gibt es da auch andere Möglichkeiten?
alias5000
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 2145

WinXP Prof SP2, Ubuntu 9.04
C/C++(Code::Blocks, VS.NET),A51(Keil),Object Pascal(D2005PE, Turbo Delphi Explorer) C# (VS 2008 Express)
BeitragVerfasst: Mo 09.10.06 12:51 
Meinst du sowas, wie zwei dynamische Arrays ineinander?

Also:
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
type

  TFirstRecord = record
    s: string; //blablabla
    strings: array of string;
  end;

....

var Records: array of TFirstRecord;


Gruß alias5000

_________________
Programmers never die, they just GOSUB without RETURN
rizor Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 209

WIN XP
Delphi 2005 PE, Visual C++
BeitragVerfasst: Mi 11.10.06 16:02 
Nein, ich brauche ein array von dem ich aber nicht weiß, wie lang es wird.
Es kann sein, dass x[i] bis 1 geht es kann aber auch bis 100 gehen.
Gibt es da eine Möglichkeit?

danke.
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8554
Erhaltene Danke: 481

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Mi 11.10.06 16:18 
Dann nimm halt dynamische Arrays :D

Also nicht so etwas:
ausblenden Delphi-Quelltext
1:
var MeinArray: Array[1..100] of string;					

sondern sowas:
ausblenden Delphi-Quelltext
1:
2:
3:
var MeinArray: Array of String;
// und später in der Prozedur:
setlength(MeinArray, 100)
Zu beachten ist dabei nur, dass dynamische Arrays immer bei 0 anfangen, d.h. wenn die Länge 100 ist, dann geht der Index von 0 bis 99.

_________________
We are, we were and will not be.
rizor Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 209

WIN XP
Delphi 2005 PE, Visual C++
BeitragVerfasst: Mi 11.10.06 17:45 
Das Problem ist aber, dass ich net weiß wie lang die arrays sind.
Es kann sogar nochlänger sein.
was ist, wenn die arraymenge nicht ausgenutzt wird?
dann benötigt das prog doch unnötig speicherkapazität, oder?
Gausi
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 8554
Erhaltene Danke: 481

Windows 7, Windows 10
D7 PE, Delphi XE3 Prof, Delphi 10.3 CE
BeitragVerfasst: Mi 11.10.06 18:17 
Das ist richtig, aber wenn das Programm läuft, dann wird doch das Programm irgendwie wissen, wie lang das Array sein soll, oder? Dann ersetze im Setlength Befehl die 100 einfach durch eine Integer-Variable, die das Programm zur Laufzeit irgendwie sinnvoll füllt. Z.B.
ausblenden Delphi-Quelltext
1:
2:
n := StrToInt(Edit_AnzahlStrings.Text);
setlength(MeinArray, n);

_________________
We are, we were and will not be.
rizor Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 209

WIN XP
Delphi 2005 PE, Visual C++
BeitragVerfasst: Mi 11.10.06 21:57 
Das hilft.

danke.
rizor Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 209

WIN XP
Delphi 2005 PE, Visual C++
BeitragVerfasst: Mi 01.11.06 14:41 
ICh bekomme die Meldung, dass es inkompatible Typen sind.
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
if length(a)=am then
begin
   Inc(am);
   setlength(a, am);
end;
a[am]:=s[i];
azubi_20
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 593

WinXP SP2, Ubuntu 8.4
D7 Enterp., D2005 Prof., Java (Eclipse 3.4.0)
BeitragVerfasst: Mi 01.11.06 14:48 
ist am eine Integer-Variable ?

außerdem muss es so heißen :
ausblenden Delphi-Quelltext
1:
a[am-1]:=s[i];					

Begründung :
am ist die länge, z.B. 10
erstes Element hat den index 0 und NICHT 1
daher hat das letzte elemtent den index 9, bzw. allgemein : länge - 1
rizor Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 209

WIN XP
Delphi 2005 PE, Visual C++
BeitragVerfasst: Mi 01.11.06 15:32 
Ja, am ist ein integer
azubi_20
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 593

WinXP SP2, Ubuntu 8.4
D7 Enterp., D2005 Prof., Java (Eclipse 3.4.0)
BeitragVerfasst: Mi 01.11.06 15:34 
wo genau tritt denn der Fehler im Code auf ?
rizor Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 209

WIN XP
Delphi 2005 PE, Visual C++
BeitragVerfasst: Mi 01.11.06 15:42 
ausblenden Delphi-Quelltext
1:
setlength(a, am);					

Der Fehler lautet:
[Fehler] Unit1.pas(56): E2008 Inkompatible Typen
azubi_20
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 593

WinXP SP2, Ubuntu 8.4
D7 Enterp., D2005 Prof., Java (Eclipse 3.4.0)
BeitragVerfasst: Mi 01.11.06 15:47 
a ist ein array ?
am ist eine Variable vom typ integer die belegt ist ?
rizor Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 209

WIN XP
Delphi 2005 PE, Visual C++
BeitragVerfasst: Mi 01.11.06 15:54 
ja.
a ist ein array und am hat mindestens den wert eins.
azubi_20
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 593

WinXP SP2, Ubuntu 8.4
D7 Enterp., D2005 Prof., Java (Eclipse 3.4.0)
BeitragVerfasst: Mi 01.11.06 15:57 
hm, komisch - dann müsste das eigentlich funktionieren.
Zeig mal den ganzen code.
rizor Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 209

WIN XP
Delphi 2005 PE, Visual C++
BeitragVerfasst: Mi 01.11.06 15:58 
da:

ausblenden volle Höhe 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:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
procedure TForm1.spalten(var am:integer);
  var i,l:integer;
      s:string;
begin
  l:= length(efunktion.text);
  s:=efunktion.text;
  am:=0;
  for i:=1 to l do
  begin
    if (s[i]<>'+')and(s[i]<>'-')then //Kontrolle ob Zahl oder Variable
    begin
      if a[am]=#0 then     //kontrolle ob array leer ist

        a[am]:=s[i]     //wenn ja, dann beschreiben
      else
        a[am]:=a[am]+s[i];  //ansonsten anfügen an einträge
    end
    else  //Wenn i ein '+' or '-' enthält dann
    begin
      if (i>1)then  //Wenn mitten in der Funktion dann nächstes Teil in nächstem array speichern
      begin
        if length(a)=am then
        begin
          Inc(am);
          setlength(a, am);
        end;
        a[am]:=s[i];
      end
      else
        if s[i]<>'+' then  //wenn am anfang dann gucken ob i '-' enthält
          a[am]:=s[i];    //erstes array beschreiben
    end;
  end;
end;
rizor Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 209

WIN XP
Delphi 2005 PE, Visual C++
BeitragVerfasst: Mi 01.11.06 15:59 
a geht bei mir mindestens von 0 bis 2
Martok
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 3661
Erhaltene Danke: 604

Win 8.1, Win 10 x64
Pascal: Lazarus Snapshot, Delphi 7,2007; PHP, JS: WebStorm
BeitragVerfasst: Mi 01.11.06 16:03 
Wie hast du denn a deklariert?

(Es gibt auch ne Edit-Funktion...)

_________________
"The phoenix's price isn't inevitable. It's not part of some deep balance built into the universe. It's just the parts of the game where you haven't figured out yet how to cheat."
rizor Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 209

WIN XP
Delphi 2005 PE, Visual C++
BeitragVerfasst: Mi 01.11.06 16:07 
a ist ein array of string.
Ja, er soll das edit-feld aufspalten und in arrays speichern
azubi_20
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 593

WinXP SP2, Ubuntu 8.4
D7 Enterp., D2005 Prof., Java (Eclipse 3.4.0)
BeitragVerfasst: Mi 01.11.06 16:16 
hast du a auch initialisiert ?

Du musst zuerst wenigstens für ein Elemnet im Array Platz machen, also irgendwo an den Anfang :

ausblenden Delphi-Quelltext
1:
SetLength (a,1);					


ausserdem setzt du innerhalb deiner Prozedur "am" immer wieder zurück auf 0.
ausblenden Delphi-Quelltext
1:
am:=0;					

Das muss raus.

zudem erinnere ich noch einmal daran :
user profile iconazubi_20 hat folgendes geschrieben:

außerdem muss es so heißen :
ausblenden Delphi-Quelltext
1:
a[am-1]:=s[i];					

Begründung :
am ist die länge, z.B. 10
erstes Element hat den index 0 und NICHT 1
daher hat das letzte elemtent den index 9, bzw. allgemein : länge - 1