再使用泛型的时候,经常需要用到遍历功能:

只要继承了 TEnumerator 或 TEnumerable 这两个抽象类的 都具有遍历功能。

当然没有继承这两个抽象类的 也具有使用 for in 来遍历的功能,编译器内置的,具体可以参见万一的博客:

http://www.cnblogs.com/del/archive/2008/11/12/1332011.html

举例:

unit Unit5;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, System.Generics.Collections,
Vcl.StdCtrls; type
TForm5 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; /// <summary>
/// 定义一个结构体
/// </summary>
RPerson = record
name: string;
age: Integer;
end; var
Form5: TForm5; implementation {$R *.dfm} procedure TForm5.Button1Click(Sender: TObject);
var
mykey,myValue: string;
MyDic: TDictionary<string, string>;
MyDic2: TDictionary<string, RPerson>;
I: Integer;
person: RPerson;
begin
MyDic := TDictionary<string, string>.Create();
MyDic2 := TDictionary<string, RPerson>.Create();
try
//初始化
Memo1.Lines.Clear;
MyDic.Add('XiaoLi', '李飞刀');
MyDic.Add('XiaoWang', '王中王');
MyDic.Add('XiaoZhang', '张飞'); person.name := '小李飞刀';
person.age := ;
MyDic2.Add('XiaoLi', person);
person.name := '火云邪神';
person.age := ;
MyDic2.Add('XiaoHuo', person); //通过key来遍历
for mykey in MyDic.Keys do
begin
Memo1.Lines.Add(mykey);
end;
Memo1.Lines.Add(''); //通过value来遍历
for myValue in MyDic.Values do
begin
Memo1.Lines.Add(myValue);
end;
Memo1.Lines.Add(''); //通过结构体的值来遍历
for person in MyDic2.Values do
begin
Memo1.Lines.Add(person.name);
end;
finally
MyDic.Free;
MyDic2.Free;
end;
end; end.

可见 遍历 的思想不能 仅仅局限于传统的 for i = 0 to list.cout -1 这种方法,而是应该多用 for in ,for in 可以遍历 一切, 传统的 for 循环  for in 都能实现。

遍历 可以 直接遍历 一切(基本类型、结构体、动态数组、类对象) 既然这样,那么问题 又来了 谁的效率高呢,我们来PK下。

unit Unit5;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, System.Generics.Collections; type
TForm5 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; var
Form5: TForm5; implementation {$R *.dfm} procedure TForm5.Button1Click(Sender: TObject);
var
MyList,tempList1,tempList2: TStringList;
I: Integer;
start_time: Int64;
tempStr: string;
begin
MyList := TStringList.Create;
tempList1 := TStringList.Create;
tempList2 := TStringList.Create;
try
//先加进数据来
for I := to do
begin
MyList.Add(I.ToString);
end; start_time := GetTickCount;
for I := to MyList.Count - do
begin
//只有使用才能看到效果
tempList1.Add(MyList[I]);
end;
Memo1.Lines.Add('fot to 耗时:' + (GetTickCount - start_time).ToString); start_time := GetTickCount;
for tempStr in MyList do
begin
//只有使用才能看到效果
tempList2.Add(tempStr);
end;
Memo1.Lines.Add('fot in 耗时:' + (GetTickCount - start_time).ToString);
finally
MyList.Free;
tempList1.Free;
tempList2.Free;
end;
end; end.

效率差不多,其它的就不测试了。 总之以后 不要把思维局限于 for to 而是 多用 for in

最新文章

  1. [转]ASP.NET MVC Dynamic Themes
  2. Nginx系列3之Nginx+tomcat
  3. mysql 常用sql操作语句
  4. c#比较器 排序
  5. aptana studio 3支持jquery
  6. linux 查看外网IP
  7. 初识QML学习机制
  8. Linux下用命令格式化U盘
  9. ECMAScript6之let与const关键字
  10. GPRS优点介绍及GPRS上网相关知识(转)
  11. [转]ios 数据的传递
  12. HDU 2063 过山车(模板—— 二分图最大匹配问题)
  13. json格式处理及扩展
  14. sql学习内容记录
  15. 二进制补码除法——计算机底层整数除法模拟之Java实现
  16. 多线程tips(面试常用)
  17. Vue.js使用v-show和v-if的注意事项
  18. 取得MapReduce的Thread Dump
  19. HBuilder开发移动App——manifest.json文件解析
  20. Swift开发经验&mdash;&mdash;外部参数名

热门文章

  1. 【bzoj3926】【Zjoi2015】诸神眷顾的幻想乡
  2. SpringMVC 使用@ResponseBody返回json 中文乱码
  3. 将SQL Server账户对应到Windows系统账户
  4. Ansible lineinfile模块详解
  5. RACCommand
  6. 科学计算三维可视化---Mayavi入门(Mayavi库的基本元素和绘图实例)
  7. ElastAlert监控日志告警Web攻击行为
  8. 关于NaN
  9. python 常用对linux系统文件及目录的操作
  10. Windows上安装QT4后更改MinGW的路径