So why not use a static array? I thought that every time you change the size of a dynamic array it get re-allocated in memory. The whole array then gets copied from old loc to new loc in memory. This takes time. If you know that you will only want 20 items max then use a 20 item static array, and keep track of the last index in an int. Write a bunch of routines to add, browse, delete etc and encapsulate them in a class. It is now a TStaticArrayList (my name, you can use it :-) ). This should take all of 30 minutes.
 
If you use an array at all and want to maintain order then you will have to copy each item down into the newly deleted item when you delete.
 
You would use a dynamic array if you thought that occasionally you migh need more than 20 items. So you also keep track of the array's max size. When you run out of space THEN re-size / re-declare / re-allocate it. A good number is double it's old size (arbitrary judgement).
 
 
-----Original Message-----
From: Jason Coley [mailto:[EMAIL PROTECTED]]
Sent: Friday, 7 February 2003 4:54 p.m.
To: Multiple recipients of list delphi
Subject: RE: RE: [DUG]: Delete a variable from a Dynamic Array

I’m only talking about 1 – 20 groups of the same type max, the deletion of a item isn’t a big deal a small section of code to move items down is fine because of the small amount of items. I just thought the dynamic array would be the best way to do what I want, apart from the fact of the corruption that I am getting.

 

Jay

 

-----Original Message-----
From: Paul Heinz [mailto:[EMAIL PROTECTED]]
Sent: Friday, 7 February 2003 4:14 p.m.
To: Multiple recipients of list delphi
Subject: RE: [DUG]: Delete a variable from a Dynamic Array

 

Kyley Harris wrote:

This is why you are better off using object such as TCollection and TCollection item. there is no delete from the middle. shift all the memory down and setlength minus one 

It all depends on your access pattern. For simple groups of  simple types, or records that you create, pass around a few places, and then discard, a dynamic array is much cheaper - one call to allocate/deallocate lots of entries - rather than lots of individual calls to New and Dispose and/or Create. They're also reference counted for you (copy on write) and Delphi takes care of all the memory management.

 

If order within the array is important and insertion/deletion in the middle is common (so you can't move a single entry and chop), their performance is O(N) so you're better of with a linked list or similar indirect collection which has OO(log N) or O(1) insert/delete behaviour.

 

TTFN,

  Paul.

 

 

 

 

 

 

Reply via email to