一、遍历 TStrings
var
List: TStrings;
s: string;
begin
List := TStringList.Create;
List.CommaText := 'aaa,bbb,ccc';
for s in List do
ShowMessage(s);
List.Free;
end;
二、遍历数组
var
Arr: array[0..2] of Byte;
i: Integer;
b: Byte;
begin
for i := Low(Arr) to High(Arr) do
Arr[i] := Random(MAXBYTE);
for b in Arr do
ShowMessage(IntToStr(b));
end;
三、遍历子界
{例1}
var
sub: 0..9;
str: string;
begin
str := '';
for sub in [Low(sub)..High(sub)] do
str := str + IntToStr(sub);
ShowMessage(str); {0123456789}
end;
{例2}
type
TSub = 'A'..'G';
var
sub: TSub;
str: string;
begin
str := '';
for sub in [Low(sub)..High(sub)] do
str := str + sub;
ShowMessage(str); {ABCDEFG}
end;
{例3}
var
sub: Byte; {Byte 应该算是个 0..255 的子界}
num: Cardinal;
begin
num := 0;
for sub in [Low(sub)..High(sub)] do
Inc(num, sub);
ShowMessage(IntToStr(num)); {32640}
end;
四、遍历枚举
type
TEnum = (Red,Green,Blue);
var
enum: TEnum;
count: Integer;
begin
count := 0;
for enum in [Low(enum)..High(enum)] do
Inc(count);
ShowMessage(IntToStr(count)); {3}
end;
五、遍历集合
type
TEnum = (Red,Green,Blue,Yellow);
TSet = set of TEnum;
var
set1: set of TEnum;
set2: TSet;
elem: Tenum;
count: Integer;
begin
set1 := [Red, Yellow];
count := 0;
for elem in set1 do Inc(count);
ShowMessage(IntToStr(count)); {2}
set2 := [Red..Yellow];
count := 0;
for elem in set2 do Inc(count);
ShowMessage(IntToStr(count)); {4}
end;
六、遍历字符串
var
str: string;
c: Char;
begin
str := 'ABCD';
for c in str do
ShowMessage(c);
是不是很爽呀?哈哈,Delphi也与时俱进呀.
之前写了类助手文章中,老赵问是不是扩展方法,因为对C#没有了解到这么多,所以不知道.
那么我们在Java中要For In必须实现Iterator吧.
那么Delphi的会不会也要呢?
是的,如果我们要自己的类支持For In的话,就必须满足下面的条件:
1 必须有个公共方法GetEnumerator(),这个方法返回值是一个类,接口或者记录体.
2 上面返回的类,接口或者记录体中又必须有公共方法MoveNext(),这个方法的返回值是Boolean.
3 1中返回的类,接口或者记录体中必须有一个只读的属性Current,类型要和集合中的元素一样.
说了这么多,看个例子:
var
List: TStrings;
s: string;
begin
List := TStringList.Create;
List.CommaText := 'aaa,bbb,ccc';
for s in List do
ShowMessage(s);
List.Free;
end;
二、遍历数组
var
Arr: array[0..2] of Byte;
i: Integer;
b: Byte;
begin
for i := Low(Arr) to High(Arr) do
Arr[i] := Random(MAXBYTE);
for b in Arr do
ShowMessage(IntToStr(b));
end;
三、遍历子界
{例1}
var
sub: 0..9;
str: string;
begin
str := '';
for sub in [Low(sub)..High(sub)] do
str := str + IntToStr(sub);
ShowMessage(str); {0123456789}
end;
{例2}
type
TSub = 'A'..'G';
var
sub: TSub;
str: string;
begin
str := '';
for sub in [Low(sub)..High(sub)] do
str := str + sub;
ShowMessage(str); {ABCDEFG}
end;
{例3}
var
sub: Byte; {Byte 应该算是个 0..255 的子界}
num: Cardinal;
begin
num := 0;
for sub in [Low(sub)..High(sub)] do
Inc(num, sub);
ShowMessage(IntToStr(num)); {32640}
end;
四、遍历枚举
type
TEnum = (Red,Green,Blue);
var
enum: TEnum;
count: Integer;
begin
count := 0;
for enum in [Low(enum)..High(enum)] do
Inc(count);
ShowMessage(IntToStr(count)); {3}
end;
五、遍历集合
type
TEnum = (Red,Green,Blue,Yellow);
TSet = set of TEnum;
var
set1: set of TEnum;
set2: TSet;
elem: Tenum;
count: Integer;
begin
set1 := [Red, Yellow];
count := 0;
for elem in set1 do Inc(count);
ShowMessage(IntToStr(count)); {2}
set2 := [Red..Yellow];
count := 0;
for elem in set2 do Inc(count);
ShowMessage(IntToStr(count)); {4}
end;
六、遍历字符串
var
str: string;
c: Char;
begin
str := 'ABCD';
for c in str do
ShowMessage(c);
end;
循环次数已知时,可以用for语句实现循环结构。for语句有两种格式:递增型和递减型。
(1)递增型for语句格式
for<循环变量>:=<初值>to<终值>do<语句>;
for i:=0 to 10 do
(2)递减型for语句格式
for<循环变量>:=<初值>downto<终值>do<语句>;
其 中,“循环变量"为一个有序类型的数据,
在第一种形式中,使用的是保留字to,循环变量则从“初值"开始逐一增加到“终值";
在第二种形式中,使用的是保 留字downto,“循环变量"则从“初值"开始逐一递减到“终值",每次执行一次循环体语句。下面继续改写上述求1~1 O之和的程序代码如下:
s:=O; ,
for i:=1 to 1 0 do //for语句开始行
s:--S+i:
35
或s:=0U;
戥s:=;
for i:=1 0 downto 1 do//for语句开
S:=S+1;
使用for语句的注意事项
始行
①在循环体内不能改变循环变量的值,因为循环变量的值是在执行完一次循环后自动进行修改的。
②循环变量的值在循环结束后,一般不再引用循环变量的值。
Delphi之For In
首先我们要知道哪些类型可以用For In吧,下面就是:
- for Element in ArrayExpr do Stmt; 数组
- for Element in StringExpr do Stmt; 字符串
- for Element in SetExpr do Stmt; 集合
- for Element in CollectionExpr do Stmt; 集合
- for Element in Record do Stmt; 结构体
我们来看例子:
type
THuangJacky = (hjA,hjB,hjC,hjD);
TJackyHuang = record
a,b,c:Integer;
end;
const
stringExpr='HuangJacky';
arrayExpr:array[0..5] of Integer= (1,2,3,4,5,6);
setExpr:set of THuangJacky = [hjA,hjB,hjD];
procedure TForm1.FormCreate(Sender: TObject);
var
I:Integer;
C:Char;
D:THuangJacky;
F:TComponent;
begin
for c in stringExpr do
ShowMessage(C);
for i in arrayExpr do
ShowMessage(IntToStr(i));
for d in setExpr do
ShowMessage(IntToStr(Ord(d)));
for F in Self do
ShowMessage(f.Name);
end;
是不是很爽呀?哈哈,Delphi也与时俱进呀.
之前写了类助手文章中,老赵问是不是扩展方法,因为对C#没有了解到这么多,所以不知道.
那么我们在Java中要For In必须实现Iterator吧.
那么Delphi的会不会也要呢?
是的,如果我们要自己的类支持For In的话,就必须满足下面的条件:
1 必须有个公共方法GetEnumerator(),这个方法返回值是一个类,接口或者记录体.
2 上面返回的类,接口或者记录体中又必须有公共方法MoveNext(),这个方法的返回值是Boolean.
3 1中返回的类,接口或者记录体中必须有一个只读的属性Current,类型要和集合中的元素一样.
说了这么多,看个例子:
type
TMyIntArray = array of Integer;
TMyEnumerator = class
Values: TMyIntArray;
Index: Integer;
public
constructor Create;
function GetCurrent: Integer;
function MoveNext: Boolean;
property Current: Integer read GetCurrent;
end;
TMyContainer = class
public
function GetEnumerator: TMyEnumerator;
end;
constructor TMyEnumerator.Create;
begin
inherited Create;
Values := TMyIntArray.Create(100, 200, 300);
Index := -1;
end;
function TMyEnumerator.MoveNext: Boolean;
begin
if Index < High(Values) then
begin
Inc(Index);
Result := True;
end
else
Result := False;
end;
function TMyEnumerator.GetCurrent: Integer;
begin
Result := Values[Index];
end;
function TMyContainer.GetEnumerator: TMyEnumerator;
begin
Result := TMyEnumerator.Create;
end;
var
MyContainer: TMyContainer;
I: Integer;
Counter: Integer;
begin
MyContainer := TMyContainer.Create;
Counter := 0;
for I in MyContainer do
Inc(Counter, I);
WriteLn('Counter = ', Counter);
end.
居然发现TMyIntArray可以用Create方法来创建数组,这个下去去研究下,或者万一兄已经研究过了.