Arrays are sufficient for ArrayLists? Really??

Daniel Gibson metalcaedes at gmail.com
Mon May 16 03:19:07 PDT 2011


Am 16.05.2011 11:54, schrieb Mehrdad:
> Hi!
> 
> I posted this question on StackOverflow about D:
> 
> http://stackoverflow.com/questions/6015008/how-to-delete-an-element-
> from-an-array-in-d
> 
> and the answers are quite surprising to me.
> 
> Are arrays really supposed to be substitutes for array lists if we
> can't remove anything from them? It seems to me like that's a really
> basic feature we're missing here... it's really annoying whenever I
> find out that each of my arrays needs its own "counter" variable,
> even though it already has a length and a capacity.
> 
> Doesn't that mean we still need an ArrayList(T) type, as powerful as
> arrays are supposed to be in D?

They're called Arrays, not Lists (or ArrayLists), so way would you
expect a delete functions?
Deleting from an Array is O(n) (if it's not the last element - and in
that case you can just do "arr.length = arr.length-1;" in D), while
deleting from a typical (Linked) List is O(1), i.e. it is pretty
expensive and should be avoided, so it makes sense to not support it
directly.

IMHO arrays are not supposed to substitute ArrayLists. They're a basic
type built in the language, while ArrayLists are usually classes that
implement a list interface on top of arrays.
Because of the lack of built in dynamic arrays (in Java etc) ArrayLists
are frequently used like dynamic arrays.
So just because ArrayLists are used like dynamic arrays it doesn't mean
that dynamic arrays should behave like ArrayLists and support all their
features natively.

If you want something like an ArrayList in D, have a look at
std.container.Array :-)

Cheers,
- Daniel


More information about the Digitalmars-d mailing list