Kea*_*lls 3 delphi delphi-7 dynamic-arrays
如何找出由array of Integer?定义的某个动态数组的最小值和最大值?
例如:
Y: array of integer;
Run Code Online (Sandbox Code Playgroud)
Dav*_*nan 17
最简单的方法是使用执行此服务的内置函数.它们被调用MinIntValue,MaxIntValue并且可以在Math单元中找到.
uses
Math;
....
TheMin := MinIntValue(TheArray);
TheMax := MaxIntValue(TheArray);
Run Code Online (Sandbox Code Playgroud)
Glo*_*egg 10
在单位Math中有重载函数:
function MinValue(const Data: array of Single): Single; overload;
function MinValue(const Data: array of Double): Double; overload;
function MinValue(const Data: array of Extended): Extended; overload;
function MinIntValue(const Data: array of Integer): Integer;
function MaxValue(const Data: array of Single): Single; overload;
function MaxValue(const Data: array of Double): Double; overload;
function MaxValue(const Data: array of Extended): Extended; overload;
function MaxIntValue(const Data: array of Integer): Integer;
Run Code Online (Sandbox Code Playgroud)
因为你使用的是整数,所以你应该使用MinIntValue和MaxIntValue
您必须遍历数组,寻找所需的值,例如:
function TMyClass.GetMinValue: Integer;
var
Idx: Integer;
begin
Result := MyArray[Low(MyArray)];
for Idx := Low(MyArray)+1 to High(MyArray) do
begin
if MyArray[Idx] < Result then
Result := MyArray[Idx];
end;
end;
function TMyClass.GetMaxValue: Integer;
var
Idx: Integer;
begin
Result := MyArray[Low(MyArray)];
for Idx := Low(MyArray)+1 to High(MyArray) do
begin
if MyArray[Idx] > Result then
Result := MyArray[Idx];
end;
end;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
16618 次 |
| 最近记录: |