[Go]delphi Delete the specified element of the dynamic array

Source: Internet
Author: User

Typetarr = array of tpoint; {It is much easier to define arrays first as a type, only with Tpoint test} {The process of deleting a dynamic array specified element: The parameter arr is the array name, the parameter index is the index to be deleted}procedure Deletearritem (Var Arr:tarr; Index:integer); Varcount:cardinal;begincount: = 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; {Test}procedure Tform1.button1click (sender:tobject); vararr:tarr;i:integer;begin{test Data}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; {Delete the fourth element and view the result after deletion} Deletearritem (arr, 3); for I: = 0 to Length (arr)-1 doMemo1.Lines.Add (Format ('%d,%d ', [arr[i]. X, Arr[i]. Y]), end,--------------------------------------------------------------------------------sometimes it's easier to use a pointer to a dynamic array and simply modify it. :--------------------------------------------------------------------------------Typetarr = array of tpoint; PARR = ^tarr; {Procedure}procedure DeleTearritem (P:parr; Index:integer); Varcount:cardinal;begincount: = Length (p^), if (Count = 0) or (Index < 0) or (index >= Count) then E Xit Move (P^[index+1], P^[index], (count-index) * SIZEOF (p^[0])); SetLength (p^, Count-1); end; {Test}procedure Tform1.button1click (sender:tobject); Vararr:tarr;i:integer;beginsetlength (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 doMemo1.Lines.Add (Format ('%d,%d ', [arr[i]. X, Arr[i].  Y])); end;

[Go]delphi Delete the specified element of the dynamic array

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.