[转]delphi 删除动态数组的指定元素
type
TArr = array of TPoint; {把数组先定义成一个类型会方便许多, 这里仅用 TPoint 测试} {删除动态数组指定元素的过程: 参数 arr 是数组名, 参数 Index 是要删除的索引}
procedure DeleteArrItem(var arr: TArr; Index: Integer);
var
Count: Cardinal;
begin
Count := Length(arr);
if (Count = 0) or (Index < 0) or (Index >= Count) then Exit;
Move(arr[Index+1], arr[Index], (Count-Index)* SizeOf(arr[0]));
SetLength(arr, Count - 1);
end; {测试}
procedure TForm1.Button1Click(Sender: TObject);
var
arr: TArr;
i: Integer;
begin
{测试数据}
SetLength(arr, 5);
arr[0].X := 1; arr[0].Y := 111;
arr[1].X := 2; arr[1].Y := 222;
arr[2].X := 3; arr[2].Y := 333;
arr[3].X := 4; arr[3].Y := 444;
arr[4].X := 5; arr[4].Y := 555; {删除第四个元素, 并查看删除后的结果}
DeleteArrItem(arr, 3);
for i := 0 to Length(arr) - 1 do
Memo1.Lines.Add(Format('%d, %d', [arr[i].X, arr[i].Y]));
end;
-------------------------------------------------------------------------------- 有时使用动态数组的指针更方便, 简单修改即可:
-------------------------------------------------------------------------------- type
TArr = array of TPoint;
PArr = ^TArr; {过程}
procedure DeleteArrItem(p: PArr; Index: Integer);
var
Count: Cardinal;
begin
Count := Length(p^);
if (Count = 0) or (Index < 0) or (Index >= Count) then Exit;
Move(p^[Index+1], p^[Index], (Count-Index)* SizeOf(p^[0]));
SetLength(p^, Count - 1);
end; {测试}
procedure TForm1.Button1Click(Sender: TObject);
var
arr: TArr;
i: Integer;
begin
SetLength(arr, 5);
arr[0].X := 1; arr[0].Y := 111;
arr[1].X := 2; arr[1].Y := 222;
arr[2].X := 3; arr[2].Y := 333;
arr[3].X := 4; arr[3].Y := 444;
arr[4].X := 5; arr[4].Y := 555; DeleteArrItem(@arr, 3);
for i := 0 to Length(arr) - 1 do
Memo1.Lines.Add(Format('%d, %d', [arr[i].X, arr[i].Y]));
end;
[转]delphi 删除动态数组的指定元素的更多相关文章
- Delphi 的动态数组
传统的Pascal 语言其数组大小是预先确定的,当你用数组结构声明数据类型时,你必须指定数组元素的个数.专业程序员也许知道些许动态数组的实现技术,一般是采用指针,用手工分配并释放所需的内存. Delp ...
- 【LeetCode每天一题】Find First and Last Position of Element in Sorted Array(找到排序数组中指定元素的开始和结束下标)
Given an array of integers nums sorted in ascending order, find the starting and ending position of ...
- js 删除数组的指定元素
//为数组对象增加相应的的方法,直接调用数组的removeByValue方法即可删除指定元素 Array.prototype.removeByValue = function (val) { for ...
- Delphi泛型动态数组的扩展--转贴
此文章转载于http://www.raysoftware.cn/?p=278&tdsourcetag=s_pcqq_aiomsg的博客 从Delphi支持泛型的第一天起就有了一种新的动态数组类 ...
- JavaScript 删除某个数组中指定的对象和删除对象属性
Javascript: 删除指定对象:使用过程中只适合删除对象,如果数组中添加的是类型Function的话是删除不了的. function removeObjWithArr(_arr,_obj) { ...
- JavaScript 删除某个数组中指定的对象
返回对象在数组中的下标: _arr表示一个Array数组,里面包括了很多的对象如下图: _obj表示某一个数组对象 function getIndex (_arr,_obj) { var le ...
- 删除php数组中的元素
删除一个元素,且保持原有索引不变 使用 unset 函数,示例如下: <?php $array = array(0 => "a", 1 => "b&qu ...
- LeetCode 27 Remove Element (移除数组中指定元素)
题目链接: https://leetcode.com/problems/remove-element/?tab=Description Problem : 移除数组中给定target的元素,返回剩 ...
- javaScript 删除数组中指定元素
Array.prototype.indexOf = function(val) { for (var i = 0; i < this.length; i++) { if (this[i] == ...
随机推荐
- -_-#【Better JS Code】严格模式
要在整个脚本中启用严格模式,可以在顶部添加如下代码: "use strict"; 这行代码看起来像是字符串,而且也没有赋值给任何变量,但其实它是一个编译指示,用于告诉支持的 Jav ...
- 数据结构:(平衡树,链表)BZOJ 1588[HNOI2002]营业额统计
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 12173 Solved: 4354[Submit][Sta ...
- MFC UpdateData(true) 失败原因
关于MFC UpdateData的介绍SurpassLi博主在http://www.cnblogs.com/lidabo/archive/2012/07/17/2595464.html 已经介绍的很 ...
- 动态规划——K背包问题
Problem DescriptionNow you are asked to measure a dose of medicine with a balance and a number of we ...
- 用phpQuery像jquery一样解析html代码
简介 如何在php中方便地解析html代码,估计是每个phper都会遇到的问题.用phpQuery就可以让php处理html代码像jQuery一样方便. 项目地址:https://code.googl ...
- 使用JAVA与SmartFoxServer来实现游戏服务器概述
SmartFoxServer 是专门为Adobe Flash设计的跨平台socket服务器,让开发者高效地开发多人应用及游戏. 该服务器主要用来创建多玩家游戏.并提供强大的制作工具,各种回合制游戏和实 ...
- Confluence5.1 最新版的安装&破解&汉化
转自 博客 http://seanzhou.iteye.com/blog/1287016 亲自基本上按照这个教程装了一遍,成功运行. 1.介绍 Atlassian Confluence(简称Confl ...
- C语言判断系统数据大/小端存储方式
小端存储:数据的低位部分,存储于存储器的低地址空间里. 大端存储:数据的低位部分,存储于存储器的高地址空间里. 首先,一般PC数据存储方式是小端存储. 基本实现思想是:将存储器中所存的数据按字节以地址 ...
- media screen 响应式布局(知识点)
一.什么是响应式布局? 响应式布局是Ethan Marcotte在2010年5月份提出的一个概念,简而言之,就是一个网站能够兼容多个终端--而不是为每个终端做一个特定的版本.这个概念是为解决移动互联网 ...
- MongoDB与传统数据库的使用区别——批量插入与批量查询
我在百X知道上回答问题时经常遇到类似与这样的问题:MongoDB有没有像MySQL一样的ODBC驱动?MongoDB能不能像MySQL一样获取字段名称或类型. 我的回答是:不行,因为MongoDB不是 ...