全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:2132
推到 Plurk!
推到 Facebook!

Array問題

尚未結案
tommylitang
一般會員


發表:12
回覆:0
積分:2
註冊:2004-12-12

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-12-15 18:29:11 IP:219.78.xxx.xxx 未訂閱
Is there any function which searchs for an element in an array and returns its position?
pcboy
版主


發表:177
回覆:1838
積分:1463
註冊:2004-01-13

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-12-16 10:11:23 IP:210.69.xxx.xxx 未訂閱
IndexOf method (TBaseArray) Returns the position of the first element with a specified value. Delphi syntax: function IndexOf( var Item ): Integer; virtual; C syntax: virtual int __fastcall IndexOf(void *Item); Description Call IndexOf to obtain the position of the first occurrence of the value Value. IndexOf returns the 0-based index of the element. Thus, if Value matches the first element in the array, IndexOf returns 0, if Value matches the second element, IndexOf returns 1, and so on. If the value is not in the array, IndexOf returns -1. Note: IndexOf does not work for arrays sorted in descending order.
------
能力不足,求助於人;有能力時,幫幫別人;如果您滿意答覆,請適時結案!

子曰:問有三種,不懂則問,雖懂有疑則問,雖懂而想知更多則問!
jow
尊榮會員


發表:66
回覆:751
積分:1253
註冊:2002-03-13

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-12-16 10:17:52 IP:220.130.xxx.xxx 未訂閱
就我個人的經驗,在delphi中好像沒有現成的單一函數提供用作搜尋,在VCL提 供的類別裡才有,並且你要施以搜尋的Array是否為排序過的資料,則關係著你 的solution考慮的方向.    在資料未排序的狀態下,毫無疑問的,只有採取循序搜尋了;而對於已排序的資 料你可以實作你認為適合的搜尋函數.    以下是個人在資料封裝,排序,搜尋上的一些經驗,謹供參考
unit Unit1;    interface    uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;    type
  TMyRec = packed record
    K: WORD;//Key Field
    A: Integer;
    B: Integer;
  end;      TMyData = class(TPersistent)
  private
    function GetKey: string;
  public
    Rec: TMyRec;
    property Key: string read GetKey;//Key For Sorting
  end;      TMyDataList = class(TStringList)
  private
    function GetData(Index: Integer): TMyData;
  public
    constructor Create;
    destructor Destroy; override;
    procedure Clear; override;
    procedure Delete(Index: Integer); override;
    function AddData(AData: TMyData): Integer;
    property Data[Index: Integer]: TMyData read GetData; default;
  end;      TForm1 = class(TForm)
    Button1: TButton;
    ListBox1: TListBox;
    Button2: TButton;
    Button0: TButton;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button0Click(Sender: TObject);
  protected
    MyDataList: TMyDataList;
  end;    var
  Form1: TForm1;    implementation    {$R *.dfm}    { TMyData }    function TMyData.GetKey: string;
begin
  Result := Format('%5.5d', [Rec.K]);
end;    { TMyDataList }    constructor TMyDataList.Create;
begin
  inherited;
  Duplicates := dupIgnore;
  Sorted := True;
end;    destructor TMyDataList.Destroy;
begin
  Clear;
  inherited;
end;    function TMyDataList.AddData(AData: TMyData): Integer;
begin
  //此處的Find就是一種搜尋的方法(Method)
  if not Find(AData.Key, Result) then//物件不存在,將它Add到List中
    Result := AddObject(AData.Key, AData)
  else begin//物件已存在,將物件內含值更新,並Free傳入的物件
    TMyData(Objects[Result]).Rec := AData.Rec;
    AData.Free;
  end;
end;    procedure TMyDataList.Clear;
begin
  //清除所有物件
  while Count > 0 do Delete(0);
  inherited;
end;    procedure TMyDataList.Delete(Index: Integer);
begin
  if Assigned(Self[Index])then
    Self[Index].Free;
  inherited;
end;    function TMyDataList.GetData(Index: Integer): TMyData;
begin
  if(Index>-1)and(Index -1 then
    begin
      ListBox1.Clear;
      ListBox1.Items.Add('Key=' MyDataList[Index].Key);
      ListBox1.Items.Add('A=' IntToStr(MyDataList[Index].Rec.A));
      ListBox1.Items.Add('B=' IntToStr(MyDataList[Index].Rec.B));
    end
    else ListBox1.Items.Text := 'not found';
  end;
end;    end.
系統時間:2024-05-22 6:47:12
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!