how to turn ICollection into ArrayList?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

since ArrayList implements ICollection, is there a quick way to convert an
ICollection into ArrayList (besides actually iterating through each element
in the ICollection and explicitly adding them to a new ArrayList instance) ??
 
AddRange simply iterates the collection and copies the items.

MrNobody, I guess the question that I have is, when you refer to "quick",
are you talking about the fewest number of code lines, or the fewest number
of program cycles.

There is no way to automatically turn an ICollection into an ArrayList
without copying the items, unless the collection was originally an
ArrayList. But the simplest way might be to use AddRange like Cor mentioned
below.
 
Doh!
I should have given a C# sample :-|

ICollection collection;
ArrayList list = new ArrayList(collection);

Jay
 
Yeah I was kinda hoping for something that used least cpu cycles- like a type
cast , butt it's ok because I don't really have THAT many items in this
collection... Thanks everyone
 
Hi,

A shallow copy is the only way of doing it, as ArrayList has its own
internal way to keep the data. Also with a copy the original collection can
be modified afterwards and the arraylist will not be changed.

Cheers,
 
Back
Top