Delphi中有沒有刪除數組中某個指定元素的函數? ( 积分: 50 )

  • 主题发起人 主题发起人 Jelly0228
  • 开始时间 开始时间
J

Jelly0228

Unregistered / Unconfirmed
GUEST, unregistred user!
難道俺只能將需要的元素Copy到另外一個數組中不成?
 
難道俺只能將需要的元素Copy到另外一個數組中不成?
 
应该没有的.
你也可以将要删除的后面的值都向前移一位,再把数组长度减小.
 
謝謝oiwin,請問如何減小數組長度?
 
Look This
====================
unit xdarray;

interface

uses SysUtils, Windows, Dialogs, Math;

// A: 笆篈皚?跑计 elSize: じ?┮??じ舱 Index: 饼?埃?じ??ま
// Count: 饼?埃?じ?计ヘ
procedure DynArrayDelete(var A
elSize: Longint
index, Count: Integer);
procedure DynArrayInsert(var A
elSize: Longint
index, Count: Integer);

procedure DynArrayCopy(var ADst
const ASrc
elSize: Longint
IndexDst, IndexSrc, Count: Integer);
procedure DynArrayAppend(var ADst
const ASrc
elSize: Longint
IndexSrc, Count: Integer);

implementation

procedure DynArraySetZero(var A);
var
P: PLongint;
begin
P := PLongint(A);
Dec(P);
P^ := 0;
Dec(P);
P^ := 0;
end;

procedure DynArrayDelete(var A
elSize: Longint
index, Count: Integer);
var
len, MaxDelete: Integer

P : PLongint;
begin
P := PLongint(A);
if P = nil then Exit;

len := PLongint(PChar(P) - 4)^;

if index >= len then Exit;

MaxDelete := len - index;
Count := Min(Count, MaxDelete);

if Count = 0 then Exit
// nothing to delete

Dec(len, Count);
MoveMemory(PChar(P) + index * elSize, PChar(P) + (index + Count) * elSize, (len - index) * elSize);

Dec(P);
Dec(P);
ReallocMem(P, len * elSize + Sizeof(Longint) * 2);
Inc(P);
P^ := len
// new length
Inc(P);

PLongint(A) := P;
end;

procedure DynArrayInsert(var A
elSize: Longint
index, Count: Integer);
const
PNull: Pointer = nil;
var
len, I: Integer

P : PLongint;
begin
P := PLongint(A);
if P <> nil then
begin
len := PLongint(PChar(P) - 4)^;
if (index > len) or (Count = 0) then Exit
// nothing to insert

Dec(P);
Dec(P);
end else len := 0;

ReallocMem(P, (len + Count) * elSize + Sizeof(Longint) * 2)
// ? realloc
Inc(P);
P^ := len + Count;
Inc(P);

MoveMemory(PChar(P) + (index + Count) * elSize, PChar(P) + index * elSize, (len - index) * elSize)
// ┕??
for I := index to index + Count - 1 do
System.Move(PNull, PChar(Integer(P) + I * elSize)^, elSize);

PLongint(A) := P;
end;

procedure DynArrayCopy(var ADst
const ASrc
elSize: Longint
IndexDst, IndexSrc, Count: Integer);
var
PDst, PSrc : PLongint;
LenDst, LenSrc: Integer

begin
PDst := PLongint(ADst);
PSrc := PLongint(ASrc);
if (PSrc = nil) then Exit;

if PDst <> nil then
LenDst := PLongint(PChar(PDst) - 4)^
else LenDst := 0;

LenSrc := PLongint(PChar(PSrc) - 4)^;

Count := Min(LenSrc - IndexSrc, Count)
// src array ぃì?, 搭ぶ count

if IndexDst + Count - 1 > LenDst then // dst array ?丁ぃì
begin
DynArrayInsert(ADst, elSize, IndexDst, (IndexDst + Count) - LenDst)
// 干ì, ず甧ぃ恨, ?穦碞籠奔?
PDst := PLongint(ADst);
end;

MoveMemory(PChar(PDst) + IndexDst * elSize, PChar(PSrc) + IndexSrc * elSize, Count * elSize)
// 狡籹
end;

procedure DynArrayAppend(var ADst
const ASrc
elSize: Longint
IndexSrc, Count: Integer);
var
P: PLongint;
begin
P := PLongint(ADst);
if (P = nil) then Exit;

DynArrayCopy(ADst, ASrc, elSize, PLongint(PChar(P) - 4)^, IndexSrc, Count);
end;

end.
 
我的想法是用动态数组,SETLENGTH改变数组长度,其实动态数很方便的.
//写一个示例你看看.
procedure TForm1.Button2Click(Sender: TObject);
var
test:array of Integer;
i:Integer;
begin
SetLength(test,5);
//给数组随便赋个值
for i:=0 to 4 do
test:=i+1;
//现在我想删除第2个
for i:=1 to 3 do
test:=Test+1;
SetLength(test,4);
//这样就删除完毕了.显示出来看一下吧.
for i:=0 to 3 do
edit1.Text:=Text+IntToStr(test);
end;
 
多人接受答案了。
 

Similar threads

S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
869
import
I
D
回复
0
查看
1K
DelphiTeacher的专栏
D
I
回复
0
查看
813
import
I
后退
顶部