reverse a collection

G

Guest

i have a colleciton that i need to reverse. Array has a reverse method. Is
their any direct way to transfer the items in the collection to an array or
arraylist, reverse the array and transfer back them to the collection.
Or looping through each element is the only option.

TIA
nafri
 
J

Jay B. Harlow [MVP - Outlook]

Nafri,
It depends on what kind of collection you have, ArrayList has a ToArray
method, if your collection is based on ArrayList either directly or
indirectly (CollectionBase) you can leverage this method.

ArrayList has AddRange & a constructor that accepts a collection, you could
indirectly (CollectionBase) leverage this methods to return the array back
to the collection.

If your collection is not based directly or indirectly on ArrayList, or have
similar methods, then you are correct, looping may be your only option.

Some collections, such as Hashtable, do not support he concept of an order,
so reversing them do not make sense per se.
Hope this helps
Jay
 
C

chanmmn

You can but you need to watch out array is the collection of the same data
type.

chanmm
 

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