查了一下,好象是TCollection,但帮助中说只能放继承自TCollectionItem的类的对象,不知道是不是这个,请高手详细说下用法:怎么添加、删除对象,怎么遍历?谢谢!

解决方案 »

  1.   

    不清楚Java中的Collection
    不过Delphi里面有TList可以放对象
      

  2.   

    有些類似的!
    delphi的, 主要是以 List 為主!
    TList, TObjectList, TStack, TQueue, TCollection
    都有
    <<delphi 深度探索>>II 中的說到!!
      

  3.   

    用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;
      

  4.   

    我看了帮助。
    基本上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;
      

  5.   

    eshow (深山含笑)  你也是java版上的啊,,来这里镀金啦:) 
    呵呵,我初学,多多关照。
      

  6.   

    To: totodo(土豆仙) 是啊!最近狂学Dephi.....
      

  7.   

    我也是:) 不过我觉得Delphi蛮有意思的。:)
      

  8.   

    TO:totodo(土豆仙):
       哎~~我觉得有些不习惯,发现Delphi很多直接调用方法和过程,而前面没有'Referrence.'也许更高级的应用才会更面向对象些吧。总之现在我还是菜鸟。
      

  9.   

    是吗?我跟你们正相反、先学的Delphi、現在正学Java,JavaScript...
    "家爸"系列、呵呵~~~”Java中的Collection类”在Delphi中、用TList最灵活,是一个基本的TYPE。
    ”可以放任何对象”、還可对其任意挿入、削除、排序...
      

  10.   

    To: eshow(深山含笑) 
    我是为了配合WebService 才学习delphi用它做Client的。 刚开始呢。Delphi 也是很OO的:看这里啊:)挺好的。
    http://www.csdn.net/develop/article/26/26264.shtm ailibuli(愛理不理) 
     哈,你也在?这里快成了java集中营了:)
      

  11.   

    TO:totodo(土豆仙)
       哈,我俩一样,我也是要做客户端!!  你给的这个连接不是java版的delva吗!!呵呵,我一直都在看。  不过我现在还对dephi的入口程序不是一个类有点晕菜!(java中是要有类,然后定义main()方法的啊!)
      

  12.   

    hehe^^ 做个标记先,一会儿再跟你们聊 哈哈
      

  13.   

    楼上的都说的TList查了一下是可以放对象。   还想请问下Delphi中有不有类似Java中HashSet或者HashTable的类,也就是可以把key和对象一起放进去,类似于name=value的形式,访问时通过key就可以得到相应的对象。
       
       请各位熟悉Delphi的说一说。
      

  14.   

    在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;(注:写的不規範、只供参考)
      

  15.   

    補充:若要象HashSet或者HashTable、要在TList自己指定KEY後、再自己做排序!還有:君要不要Java的分?俺有二個貼子↓、死菜了。
       問題自己己解決、帮UP一下、馬上結帳...
    http://expert.csdn.net/Expert/topic/2852/2852856.xml?temp=.5335657
    http://expert.csdn.net/Expert/topic/2852/2852954.xml?temp=.8467371
      

  16.   

    TO:ailibuli(愛理不理)呵呵,谢了!还是醍醐灌顶啊!!你的帖子我也顶啦!你也帮我顶一个,也是自己解决,没人给分。
    http://expert.csdn.net/Expert/topic/2971/2971060.xml?temp=.6195032
      

  17.   

    其实在Delphi中有好多控件是可以实现HashSet或者HashTable功能的hehe^^ 比如第三方 Dx 系列的 ListBox , 每个Item都有Text和Value两个值....