用TList: var aObj: TMyObj; I : Integer; begin aList.Add(aObj); ... for I := aList.Count - 1 downto 0 do begin aObj := TMyObj(aList.items[I]); ... end; end;
我看了帮助。 基本上TList 和Java中的List意义差不多。。只不过,List的实现类比如ArrayList add的是(对象引用),这里.add的是个指针。。帮助上的例子,依次在画布上添加 100,Z, 200 ,X procedure TForm1.Button1Click(Sender: TObject);type PMyList = ^AList; AList = record I: Integer; C: Char; end;var MyList: TList; ARecord: PMyList; B: Byte; Y: Word; begin MyList := TList.Create; try New(ARecord); ARecord^.I := 100; ARecord^.C := 'Z'; MyList.Add(ARecord); {Add integer 100 and character Z to list} New(ARecord); ARecord^.I := 200; ARecord^.C := 'X'; MyList.Add(ARecord); {Add integer 200 and character X to list} { Now paint the items onto the paintbox} Y := 10; {Variable used in TextOut function} for B := 0 to (MyList.Count - 1) do begin ARecord := MyList.Items[B]; Canvas.TextOut(10, Y, IntToStr(ARecord^.I)); {Display I} Y := Y + 30; {Increment Y Value again} Canvas.TextOut(10, Y, ARecord^.C); {Display C} Y := Y + 30; {Increment Y Value} end; { Cleanup: must free the list items as well as the list } for B := 0 to (MyList.Count - 1) do begin ARecord := MyList.Items[B]; Dispose(ARecord); end; finally MyList.Free; end; end;
在TList中、誰都可以当做"KEY"、自由指定。例: type PMyRecord = ^TMyRecord; TMyRecord = record Key1: string[8]; Key2: integer; Key3: String; {...} end; var MyList: TList;procedure TForm1.Button1Click(Sender: TObject); var PR: PMyRecord; begin new(PR); PR.Key1 := '00000001'; PR.Key2 := 1; PR.Key3 := 'yourName1'; MyList.Add(PR); new(PR); PR.Key1 := '00000002'; PR.Key2 := 2; PR.Key3 := 'yourName2'; MyList.Add(PR); showMessage(Find_Key('00000001').Key3); end;//自己写一個函数任意指定KEY(還可指定多数個KEY) function Find_Key(Str: String): PMyRecord; var i: integer; P: PMyRecord; begin P := nil; for i:=0 to MyList.Count-1 do begin P := MyList.Items[i]; if (P.Key1 = Str) then break; //例:指定Key1為”KEY” end; result := P; end;procedure TForm1.FormCreate(Sender: TObject); begin MyList := TList.Create; end;(注:写的不規範、只供参考)
不过Delphi里面有TList可以放对象
delphi的, 主要是以 List 為主!
TList, TObjectList, TStack, TQueue, TCollection
都有
<<delphi 深度探索>>II 中的說到!!
var
aObj: TMyObj;
I : Integer;
begin
aList.Add(aObj);
...
for I := aList.Count - 1 downto 0 do
begin
aObj := TMyObj(aList.items[I]);
...
end;
end;
基本上TList 和Java中的List意义差不多。。只不过,List的实现类比如ArrayList add的是(对象引用),这里.add的是个指针。。帮助上的例子,依次在画布上添加 100,Z, 200 ,X
procedure TForm1.Button1Click(Sender: TObject);type
PMyList = ^AList;
AList = record
I: Integer;
C: Char;
end;var MyList: TList;
ARecord: PMyList;
B: Byte;
Y: Word;
begin
MyList := TList.Create;
try
New(ARecord);
ARecord^.I := 100;
ARecord^.C := 'Z';
MyList.Add(ARecord); {Add integer 100 and character Z to list}
New(ARecord);
ARecord^.I := 200;
ARecord^.C := 'X';
MyList.Add(ARecord); {Add integer 200 and character X to list} { Now paint the items onto the paintbox}
Y := 10; {Variable used in TextOut function} for B := 0 to (MyList.Count - 1) do
begin
ARecord := MyList.Items[B];
Canvas.TextOut(10, Y, IntToStr(ARecord^.I)); {Display I}
Y := Y + 30; {Increment Y Value again}
Canvas.TextOut(10, Y, ARecord^.C); {Display C}
Y := Y + 30; {Increment Y Value}
end; { Cleanup: must free the list items as well as the list }
for B := 0 to (MyList.Count - 1) do
begin ARecord := MyList.Items[B];
Dispose(ARecord);
end;
finally
MyList.Free;
end;
end;
呵呵,我初学,多多关照。
哎~~我觉得有些不习惯,发现Delphi很多直接调用方法和过程,而前面没有'Referrence.'也许更高级的应用才会更面向对象些吧。总之现在我还是菜鸟。
"家爸"系列、呵呵~~~”Java中的Collection类”在Delphi中、用TList最灵活,是一个基本的TYPE。
”可以放任何对象”、還可对其任意挿入、削除、排序...
我是为了配合WebService 才学习delphi用它做Client的。 刚开始呢。Delphi 也是很OO的:看这里啊:)挺好的。
http://www.csdn.net/develop/article/26/26264.shtm ailibuli(愛理不理)
哈,你也在?这里快成了java集中营了:)
哈,我俩一样,我也是要做客户端!! 你给的这个连接不是java版的delva吗!!呵呵,我一直都在看。 不过我现在还对dephi的入口程序不是一个类有点晕菜!(java中是要有类,然后定义main()方法的啊!)
请各位熟悉Delphi的说一说。
type
PMyRecord = ^TMyRecord;
TMyRecord = record
Key1: string[8];
Key2: integer;
Key3: String;
{...}
end;
var MyList: TList;procedure TForm1.Button1Click(Sender: TObject);
var PR: PMyRecord;
begin
new(PR);
PR.Key1 := '00000001';
PR.Key2 := 1;
PR.Key3 := 'yourName1';
MyList.Add(PR); new(PR);
PR.Key1 := '00000002';
PR.Key2 := 2;
PR.Key3 := 'yourName2';
MyList.Add(PR); showMessage(Find_Key('00000001').Key3);
end;//自己写一個函数任意指定KEY(還可指定多数個KEY)
function Find_Key(Str: String): PMyRecord;
var i: integer;
P: PMyRecord;
begin
P := nil;
for i:=0 to MyList.Count-1 do begin
P := MyList.Items[i];
if (P.Key1 = Str) then break; //例:指定Key1為”KEY”
end;
result := P;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
MyList := TList.Create;
end;(注:写的不規範、只供参考)
問題自己己解決、帮UP一下、馬上結帳...
http://expert.csdn.net/Expert/topic/2852/2852856.xml?temp=.5335657
http://expert.csdn.net/Expert/topic/2852/2852954.xml?temp=.8467371
http://expert.csdn.net/Expert/topic/2971/2971060.xml?temp=.6195032