[转]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] == ...
随机推荐
- VC版本的MakeObjectInstance把WNDPROC映射到类的成员函数
这段时间用VC封装Windows类库,没有MakeObjectInstance处理窗口消息确实不爽,又不想使用MFC的消息映射,这玩意的效率和美观只能呵呵. 至于MakeObjectInstance是 ...
- Delphi 的运算符列表,运算符及优先级表格 good
Delphi 的运算符列表 分类 运算符 操作 操作数 结果类型 范例 算术运算符 + 加 整数,实数 整数,实数 X + Y - 减 整数,实数 整数,实数 Result - 1 * 乘 整数,实数 ...
- 【HDOJ】3088 WORM
状态压缩+BFS. /* 3088 */ #include <iostream> #include <cstdio> #include <cstring> #inc ...
- 网络流(最大流) POJ 1637 Sightseeing tour
Sightseeing tour Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 8628 Accepted: 3636 ...
- 调用系统api修改系统时间
一:截图 二:代码 using System; using System.Collections.Generic; using System.ComponentModel; using System. ...
- [LeetCode] 141&142 Linked List Cycle I & II
Problem: Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without ...
- bzoj1588 [HNOI2002]营业额统计(Treap)
1588: [HNOI2002]营业额统计 Time Limit: 5 Sec Memory Limit: 162 MBSubmit: 11485 Solved: 4062[Submit][Sta ...
- SRM 393(1-250pt)
题意:有m个人投票,每个人在心里对所有候选者排了一个序,比如“210”,则他最想投2号,如果2号已经出局他会投1号,最后投0号,否则弃权不投.选举时进行多轮投票,知道选出winner或者所有人均出局. ...
- Java 二分查找
public int binarySearch(int[] nums, int target) { int low = 0; int high = nums.length; while (low &l ...
- mac下使用brew安装svn javahl的问题
eclipse老提示javahl太久必须得1.8以上,以前不知道什么时候在/usr/bin装过1.7的svn. 1. 删除1.7的svn sudo rm /usr/bin/svn 2.使用brew安装 ...