少しだけ便利な配列を作ろう その1 では、要素の型が string である配列を作成しました。ここでは Integer 型 を要素とする配列を作成してみます。
文字列の場合には TStringList という大変便利なクラスを利用しましたが、 今回作成する配列の要素は、Integer 型ですので、TList クラスを用いて作成していきます。
ここでは、TIntArray クラスを作成し、以下のメソッドを持たせます。
unit Unit2;
interface
uses Classes, SysUtils;
type
TIntArray = class
private
fList: TList;
function GetItem(index: Integer): Integer;
procedure SetItem(index: Integer; const Value: Integer);
procedure ItemClear;
public
constructor Create;
destructor Destroy; override;
procedure Clear;
function GetCount: Integer;
procedure Push(Item: Integer);
function Pop: Integer;
procedure UnShift(Item: Integer);
function Shift: Integer;
property Items[index: Integer]: Integer read GetItem write SetItem; default;
end;
implementation
{ TIntArray }
procedure TIntArray.Clear;
begin
ItemClear;
fList.Clear;
end;
constructor TIntArray.Create;
begin
fList := TList.Create;
end;
destructor TIntArray.Destroy;
begin
ItemClear;
fList.Free;
inherited;
end;
function TIntArray.GetCount: Integer;
begin
Result := fList.Count;
end;
function TIntArray.GetItem(index: Integer): Integer;
begin
Result := PInteger(fList.Items[index])^;
end;
procedure TIntArray.ItemClear;
var
i: Integer;
begin
for i := 0 to fList.Count-1 do
Dispose(PInteger(fList.Items[i]));
end;
function TIntArray.Pop: Integer;
var
Int: Integer;
LastIndex: Integer;
begin
if fList.Count = 0 then
raise Exception.Create('インデックスを超えています');
LastIndex := fList.Count-1;
Int := PInteger(fList.Items[LastIndex])^;
Result := Int;
Dispose(PInteger(fList.Items[LastIndex]));
fList.Delete(LastIndex);
end;
procedure TIntArray.Push(Item: Integer);
var
pInt: PInteger;
begin
New(pInt);
pInt^ := Item;
fList.Add(pInt);
end;
procedure TIntArray.SetItem(index: Integer; const Value: Integer);
begin
PInteger(fList.Items[index])^ := Value;
end;
function TIntArray.Shift: Integer;
var
Int: Integer;
begin
if fList.Count = 0 then
raise Exception.Create('インデックスを超えています');
Int := PInteger(fList.Items[0])^;
Result := Int;
Dispose(PInteger(fList.Items[0]));
fList.Delete(0);
end;
procedure TIntArray.UnShift(Item: Integer);
var
pInt: PInteger;
begin
New(pInt);
pInt^ := Item;
fList.Insert(0, pInt);
end;
end.
Unit1 について
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Spin, Unit2;
type
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
SpinEdit1: TSpinEdit;
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
procedure Button4Click(Sender: TObject);
private
{ Private 宣言 }
IntArray: TIntArray;
public
{ Public 宣言 }
procedure DisplayList;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.DisplayList;
var
i: Integer;
begin
ListBox1.Clear;
if IntArray.GetCount = 0 then
begin
ListBox1.Items.Add('リストはカラです');
exit;
end;
for i := 0 to IntArray.GetCount-1 do
ListBox1.Items.Add(IntToStr(IntArray[i]));
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Button1.Caption := 'Push';
Button2.Caption := 'Pop';
Button3.Caption := 'UnShift';
Button4.Caption := 'Shift';
IntArray := TIntArray.Create;
end;
procedure TForm1.FormDestroy(Sender: TObject);
begin
IntArray.Free;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
IntArray.Push(SpinEdit1.Value);
DisplayList;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
ShowMessage(IntToStr(IntArray.Pop) + ' を取り出しました');
DisplayList;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
IntArray.UnShift(SpinEdit1.Value);
DisplayList;
end;
procedure TForm1.Button4Click(Sender: TObject);
begin
ShowMessage(IntToStr(IntArray.Shift) + ' を取り出しました');
DisplayList;
end;
end.
エラー処理は、シンプルにしてあります。