Best way to obtain a dynamic array of Int

A

azerty

Hello

I need obtain an int[] tab in my application, It is very important that the
performance would be "perfect" and I can not know the size of it before the
process to obtains different int values ...

now, I do like this :

ArrayList al = new ArrayList();
while (myCondition())
{
.....
al.Add(myInt);
}
return (int[])al.ToArray(typeof(int));


but may be you have better way to do this ?

thanks a lot for your help !!
 
C

chris-s

int count = 0;

while (myCondition())
{
count ++;
}

return new int[count];


Cheers

Chris
 
A

azerty

thanks for your answer !

but with this technic, I must do
while (myCondition())
{
....
}

Two times !

First time to know then "count value"

and second time to populate the Int[] tab values !!!

:)
 
A

azerty

After some tests

I found a better way to create my int[] instance.

in fact, I reproduce the same algo of ArrayList (see with Reflector utility)
but without boxing/unboxing (I manipulate directly int[] and not object[])


int count = 0;

while (myCondition())
{
count ++;
}

return new int[count];


Cheers

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top