Easiest methods for copying and removing +elements+ of one ArrayList to/from another?

G

Guest

I have three Arraylists that hold strings corresponding to addresses of an
e-mail list:

ArrayList masterList
ArrayList optInToMasterList
ArrayList optOutFromMasterList

I want to know the easiest way to add all of the +contents+ of one arraylist
to another.

masterList.CopyTo (optInToMasterList) does not have the intended effect: I
end up with an array nested in an array, which is quite logical, but it
isn't what I want.

optInToMasterList.Remove(optOutFromMasterList) does not seem to work either.

I'm sure I could figure out how to .Add and .Remove what I want to using
looping structures, but I would be happiest if there's already an inbuilt
method in the Framework to help me out.

Can you help?

Thank you,
-KF
 
G

gary

Is this the sort of thing you are looking for?

ArrayList AL1 = new ArrayList(3);
ArrayList AL2 = new ArrayList(3);
ArrayList AL3 = new ArrayList(6);

//Fill an arraylist with strings
AL1.Add("One");
AL1.Add("Two");
AL1.Add("Three");

//Fill another arraylist with strings
AL2.Add("Four");
AL2.Add("Five");
AL2.Add("Six");

//Combine the two arraylists into a third list
AL3.AddRange(AL1);
AL3.AddRange(AL2);
 
G

Guest

Sometimes the brute force method will save you a lot of time, especially if
it doesn't involve a lot of code (pseudocode here):

for(int i=0;i<optIntoMasterList.Length;i++)
masterList.Add(optIntoMasterList);

Cheers
Peter
 
G

Guest

Thanks to both of you. My application now works great and I know a lot more
about the ArrayLists's methods.

ArrayList newbies who discover this e-mail thread should note that the
ArrayList equivalent of the .Length property is ".Count".

-KF

Peter Bromberg said:
Sometimes the brute force method will save you a lot of time, especially
if
it doesn't involve a lot of code (pseudocode here):

for(int i=0;i<optIntoMasterList.Length;i++)
masterList.Add(optIntoMasterList);

Cheers
Peter

--
Co-founder, Eggheadcafe.com developer portal:
http://www.eggheadcafe.com
UnBlog:
http://petesbloggerama.blogspot.com




I have three Arraylists that hold strings corresponding to addresses of
an
e-mail list:

ArrayList masterList
ArrayList optInToMasterList
ArrayList optOutFromMasterList

I want to know the easiest way to add all of the +contents+ of one
arraylist
to another.

masterList.CopyTo (optInToMasterList) does not have the intended effect:
I
end up with an array nested in an array, which is quite logical, but it
isn't what I want.

optInToMasterList.Remove(optOutFromMasterList) does not seem to work
either.

I'm sure I could figure out how to .Add and .Remove what I want to using
looping structures, but I would be happiest if there's already an inbuilt
method in the Framework to help me out.

Can you help?

Thank you,
-KF
 
K

Kevin Yu [MSFT]

Hi KF,

I agree with Peter. You can also use foreach statement for the loop.

Kevin Yu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.
Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

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