独自のリストクラスで遍歴をサポート
4447 ワード
一般的なデジタルリストクラス(TnumList)で、遍歴はまだサポートされていません.
遍歴をサポートするTnumListクラス:
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 Count: Integer read FCount;
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
Memo1.Lines.Add(FloatToStr(nList[i]));
end;
nList.Free;
end;
遍歴をサポートするTnumListクラス:
unit NumList;
interface
uses SysUtils;
type
TNumList = class;
TNumEnumerator = class
private
FIndex: Integer;
FNumList: TNumList;
public
constructor Create(aNumList: TNumList);
function GetCurrent: Double;
function MoveNext: Boolean;
property Current: Double read GetCurrent;
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);
function GetEnumerator: TNumEnumerator; //!
property Count: Integer read FCount;
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.GetEnumerator: TNumEnumerator;
begin
Result := TNumEnumerator.Create(Self);
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;
{ TNumEnumerator }
constructor TNumEnumerator.Create(aNumList: TNumList);
begin
inherited Create;
FIndex := -1;
FNumList := aNumList;
end;
function TNumEnumerator.GetCurrent: Double;
begin
Result := FNumList[FIndex];
end;
function TNumEnumerator.MoveNext: Boolean;
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
Memo1.Lines.Add(FloatToStr(num));
end;
nList.Free;
end;