随笔 - 2146  文章 - 97 评论 - 11767 trackbacks - 253

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

var
  List: TStringList;

procedure TForm1.FormCreate(Sender: TObject);
begin
  List := TStringList.Create;
  List.Add('aaa');
  List.Add('bbb');
  List.Add('ccc');
  List.Add('ddd');
end;


// for 循环的一般用法
procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
  s: string;
begin
  for i := 0 to List.Count - 1 do
  begin
    s := List[i];
    ShowMessage(s);
  end;
end;


//还有这样用的
procedure TForm1.Button2Click(Sender: TObject);
var
  i: Integer;
  s: string;
begin
  for i := 0 to Pred(List.Count) do
  begin
    s := List[i];
    ShowMessage(s);
  end;
end;


//还是 for in 最方便
procedure TForm1.Button3Click(Sender: TObject);
var
  s: string;
begin
  for s in List do
  begin
    ShowMessage(s);
  end;
end;


//反向
procedure TForm1.Button4Click(Sender: TObject);
var
  i: Integer;
  s: string;
begin
  for i := List.Count - 1 downto 0 do
  begin
    s := List[i];
    ShowMessage(s);
  end;
end;


procedure TForm1.FormDestroy(Sender: TObject);
begin
  List.Free;
end;

end.

posted on 2008-01-23 01:22  万一  阅读(...)  评论(...编辑  收藏