Converting arraylist to array/array to arraylist C #

Source: Internet
Author: User
This article explains the easiest way to convert Array Object Into arraylist and the reverse. Author: Aldwin Enriquez

Aldwin.net

Posted Date: 06 Dec, 2005
. Net classes used:


System. Collections. arraylist
 
 
Introduction

Manipulating Arrays Is One among the most common task In An application development. There were times you need to use array of objects to use the power Object Properties and there were times you might like to use an arraylist For Flexibility. Sometimes switching back and forth between these two objects becomes a royal pain In The neck. This article leads you the way on how Do Things better




The hard way

Mostly beginners Do   This Conversion manually. In Case Of converting from Object Array into an arraylist, one might instantiate New Arraylist then iterates through each Object   In The array and add it to the arraylist.

Lets assume we have Object Called person. Typically here Is What Is Commonly done:

Person [] personarray = Myperson. getpersons ();

Arraylist personlist =   New Arraylist ();
Foreach (Person objperson In Personarray)
{
Personlist. Add (objperson );
}

And creating Object Array from an arraylist might be coded This Way
Person [] personarrayfromlist =   New Person [personlist. Count];
Int Arraycounter =   0 ;
Foreach (Person objperson In Personlist)
{
Personarrayfromlist. setvalue (objperson, arraycounter++);
}





The easy one

But why Do It That Way While We can just use built - In Methods Within the. NET classes.

To perform conversions from Object Array to an arraylist use the arraylist. Adapter method. This method takes an ilist to be wrapped into an arraylist. Now the procedure above can be coded like This :
Person [] personarray = Myperson. getpersons ();

Arraylist personlist = Arraylist. adapter (personarray );

To perform conversions from arraylist Object Array use the arraylist. toarray method. Now the procedure above can be coded like This :
Person [] personarrayfromlist = (Person []) personlist. toarray ( Typeof (Person ));

Don't forget the casting preceding the arraylist. toarray method, otherwise you'll Get An error message at compile time saying you can't convert that arraylist into person array.




Summary

So next time you convert Object Array to arraylist use Static Arraylist. Adapter method and doing the reverse use the toarray method of the arraylist Object .

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.