问题
I am trying to remove an item from an array. The array is not dynamic!
I found many examples on how to do it for the dynamic variant but none for the static.
example from delphi:
var
A: array of integer;
begin
...
A:=[1,2,3,4];
Delete(A,1,2); //A will become [1,4]
...
end;
example from another site:
type
TIntArray = array of Integer;
procedure DeleteArrayElement(var AArray: TIntArray; const AIndex: Integer);
begin
Move(AArray[AIndex + 1], AArray[AIndex], SizeOf(AArray[0]) * (Length(AArray) - AIndex - 1));
SetLength(AArray, Length(AArray) - 1);
end;
...
//call via
DeleteArrayElement(IntArray, 3);
...
My array is defined as 0 .. 11 so this is not dynamic(i guess)?
When I try to use the SetLength function it says incompatible types.
Any idea how to solve this?
回答1:
When you declare a static array you tell the compiler that the memory for the whole array should be allocated and retained until the program is terminated (if allocated in global space).
You cannot change the size of a static array. This is the purpose why dynamic arrays are there in Delphi.
The Embarcadero documentation for static arrays says:
If you create a static array but don't assign values to all its elements, the unused elements are still allocated and contain random data; they are like uninitialized variables.
来源:https://stackoverflow.com/questions/33732376/delete-element-from-a-static-array