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

只要继承了 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

泛型 for to/in 遍历 PK 效率;TEnumerator、TEnumerable的更多相关文章

  1. JAVA中ArrayList与LinkedList的区别以及对应List使用foreach与使用下标遍历的效率问题

    近期在做一个对接京东的电商平台,所以对各个地方的效率考虑的比较多,今天深挖了一下ArrayList与LinkedList的区别以及对应List使用foreach与使用下标遍历的效率问题,首先说一下两种 ...

  2. C#中使用泛型对照使用通用基础类型效率减少近一倍

     C#中使用泛型对照使用通用基础类型效率减少近一倍 以下是測试结果: CSharp class and generic TotalMilliseconds: 270772.9229CSharp g ...

  3. Java中HashMap(泛型嵌套)的遍历

    //Studnet package yzhou.gen03; public class Student<T> { private T score; public T getScore() ...

  4. JS 中的数组遍历方式效率比较

    JS数组遍历,基本就是for,forin,foreach,forof,map等等一些方法,以下介绍几种本文分析用到的数组遍历方式以及进行性能分析对比 第一种:普通for循环 代码如下: ; j < ...

  5. C++11中对容器的各种循环遍历的效率比较

    #include "CycleTimeTst.h" #include <string> #include <vector> #include <lis ...

  6. 使用泛型与不使用泛型的Map的遍历

    https://www.cnblogs.com/fqfanqi/p/6187085.html

  7. PHP 数组的遍历的几种方式(以及foreach与for/while+each效率的比较)

    * 使用foreach遍历数组时要注意的问题: * 1.foreach在遍历之前会自动重置指针使用其指向第一个元素,所以foreach可以多次遍历 * 2.foreach遍历完成之后,指针是没有指向数 ...

  8. 专题三、ArrayList遍历方式以及效率比较

    一.遍历方式 ArrayList支持三种遍历方式. 1.第一种,随机访问,它是通过索引值去遍历 由于ArrayList实现了RandomAccess接口,它支持通过索引值去随机访问元素. 代码如下: ...

  9. map遍历的几种方式和效率问题

    一.map遍历的效率 先创建一个map,添加好数据: Map<String, String> map = new HashMap<>();for (int i = 0; i & ...

随机推荐

  1. 解题:POI 2007 Tourist Attractions

    题面 事实上这份代码在洛谷过不去,因为好像要用到一些压缩空间的技巧,我并不想(hui)写(捂脸) 先预处理$1$到$k+1$这些点之间相互的最短路和它们到终点的最短路,并记录下每个点能够转移到时的状态 ...

  2. YBT 6 数学基础

    $补+写题ing$ 第 1 章 快速幂 序列的第 k 个数 link $solution:$ 板子 A 的 B 次方 link $solution:$ 板子 [NOIP2013] 转圈游戏 link ...

  3. boost::asio 同步&异步例子

    同步客户端: using boost::asio; io_service service; ip::tcp::endpoint ep( ip::address::from_string(); ip:: ...

  4. vue的props和$attrs

    过去我们在vue的父子组件传值的时候,我们先需要的子组件上用props注册一些属性: <template> <div> props:{{name}},{{age}} 或者 {{ ...

  5. python 中的queue, deque

    python3 deque(双向队列) 创建双向队列 import collections d = collections.deque() append(往右边添加一个元素) import colle ...

  6. enumerate 遍历numpy数组

    enumerate 遍历numpy数组 觉得有用的话,欢迎一起讨论相互学习~Follow Me 遍历一维数组 i,j 分别表示数组的 索引 和 存储的值 import numpy as np a=np ...

  7. ASP.NET IOC之 AutoFac的认识和结合MVC的使用

    这几天研究了解发现AutoFac是个牛X的IOC容器,是.NET领域比较流行的IOC框架之一,传说是速度最快的,~ 据相关资料,相关学习,和认知,遂做了一些整理 优点: 它是C#语言联系很紧密,也就是 ...

  8. php设计模式之工厂设计模式

    概念:        工厂设计模式提供获取某个对象的新实例的一个接口,同时使调用代码避免确定实际实例化基类步骤. 很多高级模式都是依赖于工厂模式. 好处:         PHP中能够创建基于变量内容 ...

  9. Java入门系列(五)JVM内存模型

    概述 根据<Java 虚拟机规范>中的说法,Java 虚拟机的内存结构可以分为公有和私有两部分. 公有指的是所有线程都共享的部分,指的是 Java 堆.方法区.常量池. 私有指的是每个线程 ...

  10. css文字超出显示省略号

    单号: white-space:nowrap; overflow:hidden; text-overflow:ellipsis; 多行: word-break: break-all; text-ove ...