## 让自己的列表类支持遍历 转

涂孟超

``````unit NumList;

interface

uses SysUtils;

type
TNumList = class
private
FCount: Integer;
FNumArray: array of Double;
function GetNum(aIndex: Integer): Double;
procedure SetNum(aIndex: Integer; aNum: Double);
public
constructor Create(aCount: Integer);
property Nums[Index: Integer]: Double read GetNum write SetNum; default;
end;

implementation

{ TNumList }

constructor TNumList.Create(aCount: Integer);
begin
inherited Create;
FCount := aCount;
SetLength(FNumArray, FCount);
end;

function TNumList.GetNum(aIndex: Integer): Double;
begin
if (aIndex < 0) or (aIndex >= FCount) then raise Exception.Create('索引越界');
Result := FNumArray[aIndex];
end;

procedure TNumList.SetNum(aIndex: Integer; aNum: Double);
begin
if aIndex >= FCount then
begin
FCount := aIndex + 1;
SetLength(FNumArray, FCount);
end;
FNumArray[aIndex] := aNum;
end;

end. //end

//调用测试:
uses NumList;

procedure TForm1.Button1Click(Sender: TObject);
var
nList: TNumList;
i: Integer;
begin
nList := TNumList.Create(5);
for i := 0 to nList.Count - 1 do //赋值
begin
nList[i] := Random(1000) / 100;
end;

Memo1.Clear;
for i := 0 to nList.Count - 1 do //取值
begin
end;
nList.Free;
end;``````

``````unit NumList;

interface

uses SysUtils;

type
TNumList = class;

private
FIndex: Integer;
FNumList: TNumList;
public
constructor Create(aNumList: TNumList);
function GetCurrent: Double;
function MoveNext: Boolean;
end;

TNumList = class
private
FCount: Integer;
FNumArray: array of Double;
function GetNum(aIndex: Integer): Double;
procedure SetNum(aIndex: Integer; aNum: Double);
public
constructor Create(aCount: Integer);
property Nums[Index: Integer]: Double read GetNum write SetNum; default;
end;

implementation

{ TNumList }

constructor TNumList.Create(aCount: Integer);
begin
inherited Create;
FCount := aCount;
SetLength(FNumArray, FCount);
end;

begin
end;

function TNumList.GetNum(aIndex: Integer): Double;
begin
if (aIndex < 0) or (aIndex >= FCount) then raise Exception.Create('索引越界'');
Result := FNumArray[aIndex];
end;

procedure TNumList.SetNum(aIndex: Integer; aNum: Double);
begin
if aIndex >= FCount then
begin
FCount := aIndex + 1;
SetLength(FNumArray, FCount);
end;
FNumArray[aIndex] := aNum;
end;

begin
inherited Create;
FIndex := -1;
FNumList := aNumList;
end;

begin
Result := FNumList[FIndex];
end;

begin
Result := FIndex < FNumList.Count - 1;
if Result then Inc(FIndex);
end;

end. //end

//调用测试
uses NumList;

procedure TForm1.Button1Click(Sender: TObject);
var
nList: TNumList;
i: Integer;
num: Double;
begin
nList := TNumList.Create(5);
for i := 0 to nList.Count - 1 do //赋值
begin
nList[i] := Random(1000) / 100;
end;

Memo1.Clear;

for num in nList do //遍历
begin
end;

nList.Free;
end;``````

### 涂孟超

Java 常用工具类 Collections 源码分析

u011240877
2017/10/26
0
0

Java集合框架为Java编程语言的基础，也是Java面试中很重要的一个知识点。这里，我列出了一些关于Java集合的重要问题和答案。 1.Java集合框架是什么？说出一些集合框架的优点？ 每种编程语言中...

jamesese
2018/08/21
0
0
40个Java集合面试问题和答案

1.Java集合框架是什么？说出一些集合框架的优点？ 每种编程语言中都有集合，最初的Java版本包含几种集合类：Vector、Stack、HashTable和Array。随着集合的广泛使 用，Java1.2提出了囊括所有集...

2015/05/12
0
0
ArrayList工作原理

kukudeku
2016/08/26
137
0
40个Java集合类面试题和答案

Java集合框架为Java编程语言的基础，也是Java面试中很重要的一个知识点。这里，我列出了一些关于Java集合的重要问题和答案。 1.Java集合框架是什么？说出一些集合框架的优点？ 每种编程语言中...

jamesese
2018/10/08
0
0

21分钟前
1
0

linux-tao
21分钟前
1
0

mikechen优知
33分钟前
2
0
vue里实现echarts中国地图

echartsDemo.vue <template> <div> <div id="chart_example"></div> </div></template><script> // 首先要 cnpm install echarts import echarts from 'echarts' import ......

Danni3
44分钟前
2
0