To IDWB() ( 五级(中级)) 信誉:99 你这是SQL语句啊,我要的是Delphi中的。To 快乐老猫(高亚男 无米下炊) 或者如何在自定义类中生成数组结果呢? function list(A:integer;B:integer):arrary of integer; var c:arrary of integer; I:integer; begin setlength(c,500); for I:=1 to 20 do begin c[I]:=a+b; end; end;我该如何调用Result呢?
500个数据处理起来应该是非常快的:function CheckExist(ABuffer: array of Integer; iCount, iValue: Integer): Boolean; var iLoop : Integer; begin Result := False; for iLoop := 0 to iCount - 1 do if ABuffer[iLoop] = iValue then begin Result := True; break; end; end;procedure CreateRandomArray(var ABuffer: array of Integer; iCount: Integer); var iLoop : Integer; iValue: Integer; begin Randomize; for iLoop := 0 to iCount - 1 do begin repeat iValue := Random(999999); until not CheckExist(ABuffer, iLoop, iValue); ABuffer[iLoop] := iValue; end; end;procedure TForm1.Button1Click(Sender: TObject); var Buffer: array of Integer; iLoop : Integer; sTemp : String; begin SetLength(Buffer, 500); CreateRandomArray(Buffer, 500); sTemp := ''; for iLoop := 0 to 499 do sTemp := sTemp + IntToStr(Buffer[iLoop]) + ' '; ShowMessage(sTemp); end;
D7 测试完毕 unit CIntLists;interface uses Contnrs, Classes; type TMyData = Integer; PMyData = ^TMyData; TIntList = class(Tobject) private FValue: array of TMyData; FCapacity: Integer; FCount: Integer; function GetValue(Index: Integer): Integer; protected procedure Grow; procedure SetCapacity(const NewCapacity: Integer); public property Capacity: Integer read FCapacity write SetCapacity; property Count: Integer read FCount; property Value[Index: Integer]: TMyData read GetValue; function Add(const NewValue: TMyData): Integer; constructor Create; overload; constructor Create(const BufLength: Integer); overload; procedure Delete(Index: Integer); destructor Destroy; override; end; var MyIntList: TIntList; implementation uses RTLConsts, Windows; { TIntList }function TIntList.Add(const NewValue: TMyData): Integer; var i: Integer; begin Result := -1; for i := 0 to Count - 1 do if FValue[i] < NewValue then Continue else if FValue[i] = NewValue then Exit else begin Result := i; if FCount = FCapacity then Grow; System.Move(FValue[i], FValue[i + 1], (FCount - i) * SizeOf(TMyData)); FValue[i] := NewValue; Inc(FCount); Exit; end;
if FCount = FCapacity then Grow; Result := FCount; FValue[Result] := NewValue; Inc(FCount); end;constructor TIntList.Create(const BufLength: Integer); begin inherited Create; SetLength(FValue, BufLength); Windows.ZeroMemory(@FValue[0], BufLength * SizeOf(TMyData)); FCapacity := BufLength; end;constructor TIntList.Create; begin inherited; FCount := 0; FCapacity := 0; end;procedure TIntList.Delete(Index: Integer); begin if (Index < 0) or (Index >= FCount) then Exit; Dec(FCount); if Index < FCount then begin System.Move(FValue[Index + 1], FValue[Index], (FCount - Index) * SizeOf(TMyData)); FValue[Index + 1] := 0; end; end;destructor TIntList.Destroy; begin inherited; end;function TIntList.GetValue(Index: Integer): TMyData; begin if (Index >= 0) and (Index < Count) then Result := FValue[Index] else Result := 0; end;procedure TIntList.Grow; var Delta: Integer; begin if FCapacity > 64 then Delta := FCapacity div 4 else if FCapacity > 8 then Delta := 16 else Delta := 4; SetCapacity(FCapacity + Delta); end;procedure TIntList.SetCapacity(const NewCapacity: Integer); begin if (NewCapacity < FCount) or (NewCapacity > MaxListSize) then TList.Error(@SListCapacityError, NewCapacity); if NewCapacity <> FCapacity then begin SetLength(FValue, NewCapacity); Windows.ZeroMemory(@FValue[FCount], (NewCapacity - FCount) * SizeOf(TMyData)); FCapacity := NewCapacity; end; end;initialization MyIntList := TIntList.Create(20); finalization MyIntList.Free; end. ============================================================== uses CIntLists; const IDI_RandomRange = 500; procedure TForm1.Button3Click(Sender: TObject); begin Close; end;procedure TForm1.btnAddClick(Sender: TObject); var Value: TMyData; begin Value := Random(IDI_RandomRange); edtValue.Text := IntToStr(Value); if MyIntList.Add(Value) = -1 then Application.MessageBox('已经存在', PChar(Application.Title)) else RefreshList; end;procedure TForm1.FormCreate(Sender: TObject); begin Randomize; end;procedure TForm1.RefreshList; var i: Integer; li: TListItem; begin with lv1.Items do begin Clear; BeginUpdate; try for i := 0 to MyIntList.Count - 1 do begin li := Add; li.Caption := IntToStr(i + 1); li.SubItems.Add(IntToStr(MyIntList.Value[i])); end; finally EndUpdate; end; end; end;
http://blog.csdn.net/zjcxc/archive/2006/08/20/1099215.aspx
你这是SQL语句啊,我要的是Delphi中的。To 快乐老猫(高亚男 无米下炊)
或者如何在自定义类中生成数组结果呢?
function list(A:integer;B:integer):arrary of integer;
var c:arrary of integer;
I:integer;
begin
setlength(c,500);
for I:=1 to 20 do
begin
c[I]:=a+b;
end;
end;我该如何调用Result呢?
var
iLoop : Integer;
begin
Result := False;
for iLoop := 0 to iCount - 1 do
if ABuffer[iLoop] = iValue then
begin
Result := True;
break;
end;
end;procedure CreateRandomArray(var ABuffer: array of Integer; iCount: Integer);
var
iLoop : Integer;
iValue: Integer;
begin
Randomize;
for iLoop := 0 to iCount - 1 do
begin
repeat
iValue := Random(999999);
until not CheckExist(ABuffer, iLoop, iValue);
ABuffer[iLoop] := iValue;
end;
end;procedure TForm1.Button1Click(Sender: TObject);
var
Buffer: array of Integer;
iLoop : Integer;
sTemp : String;
begin
SetLength(Buffer, 500);
CreateRandomArray(Buffer, 500); sTemp := '';
for iLoop := 0 to 499 do
sTemp := sTemp + IntToStr(Buffer[iLoop]) + ' ';
ShowMessage(sTemp);
end;
unit CIntLists;interface
uses
Contnrs, Classes;
type
TMyData = Integer;
PMyData = ^TMyData; TIntList = class(Tobject)
private
FValue: array of TMyData;
FCapacity: Integer;
FCount: Integer;
function GetValue(Index: Integer): Integer;
protected
procedure Grow;
procedure SetCapacity(const NewCapacity: Integer);
public
property Capacity: Integer read FCapacity write SetCapacity;
property Count: Integer read FCount;
property Value[Index: Integer]: TMyData read GetValue;
function Add(const NewValue: TMyData): Integer;
constructor Create; overload;
constructor Create(const BufLength: Integer); overload;
procedure Delete(Index: Integer);
destructor Destroy; override;
end;
var
MyIntList: TIntList;
implementation
uses
RTLConsts, Windows;
{ TIntList }function TIntList.Add(const NewValue: TMyData): Integer;
var
i: Integer;
begin
Result := -1;
for i := 0 to Count - 1 do
if FValue[i] < NewValue then
Continue
else if FValue[i] = NewValue then
Exit
else begin
Result := i;
if FCount = FCapacity then
Grow;
System.Move(FValue[i], FValue[i + 1],
(FCount - i) * SizeOf(TMyData));
FValue[i] := NewValue;
Inc(FCount);
Exit;
end;
if FCount = FCapacity then
Grow;
Result := FCount;
FValue[Result] := NewValue;
Inc(FCount);
end;constructor TIntList.Create(const BufLength: Integer);
begin
inherited Create;
SetLength(FValue, BufLength);
Windows.ZeroMemory(@FValue[0], BufLength * SizeOf(TMyData));
FCapacity := BufLength;
end;constructor TIntList.Create;
begin
inherited;
FCount := 0;
FCapacity := 0;
end;procedure TIntList.Delete(Index: Integer);
begin
if (Index < 0) or (Index >= FCount) then
Exit;
Dec(FCount);
if Index < FCount then
begin
System.Move(FValue[Index + 1], FValue[Index],
(FCount - Index) * SizeOf(TMyData));
FValue[Index + 1] := 0;
end;
end;destructor TIntList.Destroy;
begin inherited;
end;function TIntList.GetValue(Index: Integer): TMyData;
begin
if (Index >= 0) and (Index < Count) then
Result := FValue[Index]
else
Result := 0;
end;procedure TIntList.Grow;
var
Delta: Integer;
begin
if FCapacity > 64 then
Delta := FCapacity div 4
else
if FCapacity > 8 then
Delta := 16
else
Delta := 4;
SetCapacity(FCapacity + Delta);
end;procedure TIntList.SetCapacity(const NewCapacity: Integer);
begin
if (NewCapacity < FCount) or (NewCapacity > MaxListSize) then
TList.Error(@SListCapacityError, NewCapacity);
if NewCapacity <> FCapacity then
begin
SetLength(FValue, NewCapacity);
Windows.ZeroMemory(@FValue[FCount], (NewCapacity - FCount) * SizeOf(TMyData));
FCapacity := NewCapacity;
end;
end;initialization
MyIntList := TIntList.Create(20);
finalization
MyIntList.Free;
end.
==============================================================
uses
CIntLists;
const
IDI_RandomRange = 500;
procedure TForm1.Button3Click(Sender: TObject);
begin
Close;
end;procedure TForm1.btnAddClick(Sender: TObject);
var
Value: TMyData;
begin
Value := Random(IDI_RandomRange);
edtValue.Text := IntToStr(Value);
if MyIntList.Add(Value) = -1 then
Application.MessageBox('已经存在', PChar(Application.Title))
else
RefreshList;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
Randomize;
end;procedure TForm1.RefreshList;
var
i: Integer;
li: TListItem;
begin
with lv1.Items do
begin
Clear;
BeginUpdate;
try
for i := 0 to MyIntList.Count - 1 do
begin
li := Add;
li.Caption := IntToStr(i + 1);
li.SubItems.Add(IntToStr(MyIntList.Value[i]));
end;
finally
EndUpdate;
end;
end;
end;