473,327 Members | 2,074 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

how to turn ICollection into ArrayList?

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) ??
Nov 16 '05 #1
7 23497
Hi,

Did you ever saw this page?

addrange
http://msdn.microsoft.com/library/de...rangetopic.asp

I hope this helps?

Cor
Nov 16 '05 #2
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.
"Cor Ligthert" <no************@planet.nl> wrote in message
news:es**************@TK2MSFTNGP11.phx.gbl...
Hi,

Did you ever saw this page?

addrange
http://msdn.microsoft.com/library/de...rangetopic.asp
I hope this helps?

Cor

Nov 16 '05 #3
You can use the AddRange method of the ArrayList to add from an object that
implements an IList interface.

See
http://msdn.microsoft.com/library/de...rangetopic.asp

Shariq Khan
sh****@shariqkhan.com
"MrNobody" <Mr******@discussions.microsoft.com> wrote in message
news:FE**********************************@microsof t.com...
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)
??

Nov 16 '05 #4
MrNobody,
In addition to the AddRange method, I would probably simply use the
ArrayList constructor that accepts an ICollection:

Dim collection As ICollection
Dim list As New ArrayList(collection)

For details on this constructor see:

http://msdn.microsoft.com/library/de...ctortopic2.asp

Hope this helps
Jay

"MrNobody" <Mr******@discussions.microsoft.com> wrote in message
news:FE**********************************@microsof t.com...
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)
??

Nov 16 '05 #5
Doh!
I should have given a C# sample :-|

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

Jay

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:uS**************@TK2MSFTNGP15.phx.gbl...
MrNobody,
In addition to the AddRange method, I would probably simply use the
ArrayList constructor that accepts an ICollection:

Dim collection As ICollection
Dim list As New ArrayList(collection)

For details on this constructor see:

http://msdn.microsoft.com/library/de...ctortopic2.asp

Hope this helps
Jay

"MrNobody" <Mr******@discussions.microsoft.com> wrote in message
news:FE**********************************@microsof t.com...
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) ??


Nov 16 '05 #6
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
Nov 16 '05 #7
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,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"MrNobody" <Mr******@discussions.microsoft.com> wrote in message
news:BD**********************************@microsof t.com...
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

Nov 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Kent Boogaart | last post by:
Hello all, I have two simple classes: Item and ItemCollection. Item stores a label for the item and an instance of ItemCollection for all child items. ItemCollection just stores a collection of...
7
by: Lars-Erik Aabech | last post by:
Hi! I've got problems with serializing my collections of business objects. The objects themselves serialize fine, but the collections fail. I've got the following structure: Base collection...
8
by: Sam | last post by:
I’m trying to write a Set class where it’s a list that you can add an element only once. So I naturally tried to extend the ArrayList and wrote this code. The problem is that it compiles but...
9
by: TT ( Tom Tempelaere ) | last post by:
Hi At one point in my application I have a single ArrayList object that I need to break up in two Arraylist objects: the beginning part up to an index, and the ending part from a certain index. I...
6
by: Mathew Yeates | last post by:
I'm baffled by something. I want to turn an ArrayList (which stores strings) into an array of strings. I know I can use the ToArray method and pass in a type. But I can't get the code right. Any...
7
by: ORC | last post by:
Hi, How to create an ArrayList from a System.Array or inserting the System.Array into the ArrayList ? Thanks Ole
1
by: Sylvain | last post by:
Hi, I'm encountering a very simple issue with ArrayList constructor and AddRange() method overriding. I'm defining a class that extends ArrayList and contains one overriden method:...
20
by: Dennis | last post by:
I use the following code for a strongly typed arraylist and it works great. However, I was wondering if this is the proper way to do it. I realize that if I want to implement sorting of the...
5
by: Stacey Levine | last post by:
I have a webservice that I wanted to return an ArrayList..Well the service compiles and runs when I have the output defined as ArrayList, but the WSDL defines the output as an Object so I was...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.