arrays newbie

F

francois

Hi,

I have an ArrayList object and I want to transform it into an array like
int[]
I of course do not want to loop myself

Then I thought that I could do the following:

ArrayList list = new ArrayList
.... // here fill the array with integers
int j = 3;
System.Type type = j.GetType();
list.ToArray(type)

My problem is that the ToArray method take a type object in parameter. And I
do not know how to get the type of Int32 without actually having the need to
create an instance?

Can someone tell me how to transform and ArrayList into an [] in the state
of the art way?

Thanks a lot,

francois
 
M

Morten Wennevik

Hi francois,

try

int[] i = (int[])list.ToArray(typeof(Int32));

Happy coding!
Morten Wennevik [C# MVP]
 
E

Eric

Hi,

This is how i copy an ArrayList to Array:

ArrayList al = new ArrayList();
Array ar = new Array(al.Count);
al.CopyTo(ar);

Hope this helps.
 
J

Jon Skeet [C# MVP]

Eric said:
This is how i copy an ArrayList to Array:

ArrayList al = new ArrayList();
Array ar = new Array(al.Count);
al.CopyTo(ar);

Hope this helps.

Why use that when ArrayList has a perfectly good ToArray method?
 
D

Dilip Krishnan

you'd use the typeof operator to get the type generally. Hope that helps
Hi,

I have an ArrayList object and I want to transform it into an array like
int[]
I of course do not want to loop myself

Then I thought that I could do the following:

ArrayList list = new ArrayList
.... // here fill the array with integers
int j = 3;
System.Type type = j.GetType();
list.ToArray(type)

My problem is that the ToArray method take a type object in parameter. And I
do not know how to get the type of Int32 without actually having the need to
create an instance?

Can someone tell me how to transform and ArrayList into an [] in the state
of the art way?

Thanks a lot,

francois
 

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