學習 Tlist與指針的用法 用到了 Add, Count, Items

 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;

 

=======================================轉載=============================

下文來自   http://fxh7622.blog.51cto.com/63841/29548   原創   (呵呵)

procedure TForm1.Button2Click(Sender: TObject);
const
  CardType:array[0..3] of String = ('S','H','D','C');
const
  CardNums = 4;
type
  RCardrecord = record
    CardInfo:String[2];
  end;
  PCard = ^RCardrecord;
var
  t_List:TList;
  I:Integer;
  t_Sub,t_Spare:Integer;
  t_CardType,t_CardNum:String;
  p_Card:PCard;
  t_Random:Integer;
  t_CardInfo:String[8];
  Count:Integer;
begin
  t_List:=TList.Create;     //list
  for I:=1 to 52 do
  begin
    t_Sub:=I div 14;        //intger
    t_Spare:=I mod 14;      //intger
    t_CardType:=CardType[t_Sub];   //string    array of string := ('S','H','D','C')
    t_CardNum:=IntToHex(t_Spare,1); //string
    New(p_Card);                    //^
    p_Card.CardInfo:=t_CardType+t_CardNum; //
    t_List.Add(p_Card);                   //list add ^
  end;
  Randomize;
  for I:=1 to CardNums do                //for 1 to 4 do
  begin
    t_Random:=Random(t_List.Count);      //int
    p_Card:=t_List.Items[t_Random];      //^ := list.items
    t_CardInfo:=t_CardInfo+p_Card^.CardInfo;
    t_List.Delete(t_Random);
    DisPose(p_Card);
  end;
  Count:=t_List.Count;
  for I:=Count-1 downto 0 do
  begin
    p_Card:=t_List.Items[I];
    t_List.Delete(I);
    DisPose(p_Card);
  end;
  t_List.Free;
end;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值