[转]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] == ...
随机推荐
- scheme和common lisp 区别
Scheme and Common Lisp use different names for some of the basic system functions. Many Lisp program ...
- Gvim各种插件配置(windows环境下)
1.Vundle插件:https://github.com/gmarik/Vundle.vim 用于管理Vim插件,安装此插件需要系统中已安装git,参考链接:Git for Windows安装和基本 ...
- Binary Tree Preorder Traversal —— LeetCode
Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tr ...
- ARM机器码分析
我们编写的汇编程序还是不够底层,CPU都是对机器码进行操作的,所以还需要用汇编器将汇编代码转换成机器码才能被CPU处理.下面举几个例子来说说分析ARM机器码的方法. 对编译连接之后得到的ELF进行反汇 ...
- 【26】Remove Duplicates from Sorted Array
[26]Remove Duplicates from Sorted Array Given a sorted array, remove the duplicates in place such th ...
- 使用MockMvc编写spring boot的controller的测试用例
springboot自带测试模块. 注解需要: @SpringApplicationConfiguration(classes = ComputeServiceApplication.class) 这 ...
- javaIO流小结(1)
UTF-8的字节占多少个字节? 常用中文字符用utf-8编码占用3个字节(大约2万多字),超大字符集中要占4个字节.在内存中是2个字节,真正写到硬盘上面的是3个字节. GBK.GB2312汉字占2个字 ...
- JSON字符串与JSON对象的区别及转换
JSON对象是直接可以使用JQuery操作的格式,和js中的对象一样,可以用对象(类名)点出属性(方法). JSON字符串仅仅只是一个字符串,一个整体,不截取的话没办法取出其中存储的数据,不能直接使用 ...
- DataGridView的DataGridViewComboBoxColumn列点击一次,自动处于编辑状态
本文转载:http://www.cnblogs.com/Johnny_Z/archive/2012/02/12/2348235.html Winform中的DataGridView数据绑定控件有时会用 ...
- Sublime 注册码
----- BEGIN LICENSE ----- Andrew Weber Single User License EA7E-855605 813A03DD 5E4AD9E6 6C0EEB94 BC ...