从静态数组中删除元素 [英] Delete element from a static array

查看:77
本文介绍了从静态数组中删除元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从数组中删除一项。
数组不是动态的!

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.

来自delphi的示例:

example from delphi:

var
  A: array of integer;
begin
  ...
  A:=[1,2,3,4];
  Delete(A,1,2); //A will become [1,4]
  ...
end;

另一个站点的示例:

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);
...

我的数组定义为0 .. 11因此这不是动态的(我猜)?

My array is defined as 0 .. 11 so this is not dynamic(i guess)?

当我尝试使用 SetLength 函数时,它说类型不兼容。

When I try to use the SetLength function it says incompatible types.

有什么办法解决吗?

推荐答案

声明静态数组时,您告诉编译器内存应该分配并保留整个数组的值,直到程序终止(如果在全局空间中分配)。

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).

您不能更改静态数组的大小。这就是为什么在Delphi中使用动态数组的原因。

You cannot change the size of a static array. This is the purpose why dynamic arrays are there in Delphi.

静态数组的Embarcadero文档说:

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.

这篇关于从静态数组中删除元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆