www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Get specific item by index from DList

reply TheDGuy <a.b gmail.de> writes:
Hi,

i am currently programming a small game with GTKD and i have to 
use a Dlist because an array is static but i want to add user 
inputs dynamically to a list. Now i am wondering how i can get a 
specific item from that list? I read that this isn't possible but 
is it possible to convert that DList temporarily to an array to 
get a specific element? My DList just contains integer values, 
anyone who could offer a short code example?
This doesn't work:

to!Array(level,int,userInput)[i])

Level is the length of the list, int is the type and userInput is 
the DList.
Jun 22 2016
parent reply lmpo <lmpo kldjsjf.hj> writes:
On Wednesday, 22 June 2016 at 22:10:09 UTC, TheDGuy wrote:
 Hi,

 i am currently programming a small game with GTKD and i have to 
 use a Dlist because an array is static
Static ? An array is not static. a DList is only interesting when you have to insert or remove inside the list i.e not at the back. If the container grows always from the back than you should rather use an array.
Jun 22 2016
next sibling parent reply TheDGuy <a.b gmail.de> writes:
On Wednesday, 22 June 2016 at 22:25:21 UTC, lmpo wrote:
 On Wednesday, 22 June 2016 at 22:10:09 UTC, TheDGuy wrote:
 Hi,

 i am currently programming a small game with GTKD and i have 
 to use a Dlist because an array is static
Static ? An array is not static. a DList is only interesting when you have to insert or remove inside the list i.e not at the back. If the container grows always from the back than you should rather use an array.
Thanks a lot for your answer. The main reason why i switched the DList is, that i have to initialize an array with a fixed length but it is important for me that the length-value of the property of the array is not the maximum length but the amount of indexes which actually contain values. I don't know how i could do this without creating an extra variable?
Jun 22 2016
parent "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
On Wed, Jun 22, 2016 at 10:49:54PM +0000, TheDGuy via Digitalmars-d-learn wrote:
 On Wednesday, 22 June 2016 at 22:25:21 UTC, lmpo wrote:
 On Wednesday, 22 June 2016 at 22:10:09 UTC, TheDGuy wrote:
 Hi,
 
 i am currently programming a small game with GTKD and i have to
 use a Dlist because an array is static
Static ? An array is not static. a DList is only interesting when you have to insert or remove inside the list i.e not at the back. If the container grows always from the back than you should rather use an array.
Thanks a lot for your answer. The main reason why i switched the DList is, that i have to initialize an array with a fixed length but it is important for me that the length-value of the property of the array is not the maximum length but the amount of indexes which actually contain values. I don't know how i could do this without creating an extra variable?
Take a look at: https://dlang.org/spec/arrays.html In particular, at the .reserve() method and .capacity property of arrays. It sounds like what you want is to call .reserve on the array but keep its length at 0 when you first initialize it. This way you don't need to keep an "extra variable" around. T -- When solving a problem, take care that you do not become part of the problem.
Jun 22 2016
prev sibling parent "H. S. Teoh via Digitalmars-d-learn" <digitalmars-d-learn puremagic.com> writes:
On Wed, Jun 22, 2016 at 10:25:21PM +0000, lmpo via Digitalmars-d-learn wrote:
 On Wednesday, 22 June 2016 at 22:10:09 UTC, TheDGuy wrote:
 Hi,
 
 i am currently programming a small game with GTKD and i have to use
 a Dlist because an array is static
Static ? An array is not static. a DList is only interesting when you have to insert or remove inside the list i.e not at the back. If the container grows always from the back than you should rather use an array.
Yes, and arrays also have better cache coherence, and if your elements are small, using a DList will perform poorly due to poor locality. T -- Doubt is a self-fulfilling prophecy.
Jun 22 2016