Saturday, June 13, 2009

Appending ArrayList to another ArrayList

A friend of mine asked about how to append an ArrayList to another ArrayList in C#. I found out that it's an easy task as long as the type of the ArrayList is the same. Just have to typecast it to ICollection.
e.g.

ArrayList a = new ArrayList();
a.Add("Array 1");
a.Add("Array 2");

ArrayList b = new ArrayList();
b.Add("Array 3");
b.Add("Array 4");

a.AddRange((ICollection)b);
Share:

4 comments:

  1. we do have this in our programming also
    this is one of the basic ways to learn hardcore programming.

    ReplyDelete
  2. Hahaha, u do? I didn't, I only had the simple one, typecasting in primitive data type, in classes. hehehe...

    ReplyDelete
  3. thx your code.

    I have more guess...
    How to access and change arrayList's value of arrayList?

    e.g.
    ArrayList a = new ArrayList();
    ArrayList b = new ArrayList();
    b.Add("apple");
    b.Add("banana");
    b.Add("melon");

    a.Add(b); //right?

    I wanna chang "apple" -> "kiwi"

    How I do?

    ReplyDelete
  4. I don't think it's possible to change the value of an object in an ArrayList without re-adding it. You have to loop through the items and change it manually.

    ReplyDelete

About

Related Posts

Tableau Server Unable to Start due to Blocked Ports

Problem: Sometimes you might need to use different (from the default port) port on Tableau. By default, SSL is using port 443. For my case...

About Me

My photo
Is an ordinary unNoticed man, with a little knowledge to share and high dreams to achieve. I'd be glad if I can help others, 'coz the only thing for the triumph of evil is for a good man to do nothing. I like English and Chinese language, and know a little about them. Hence, for me, most things can best be expressed in Chinese, some in English, and the rest in others ^^

About Blog

You can find a lot of debugging and deploying problems while developing applications in .NET and Visual Basic here. There are also some querying tips in SQL and typical source codes which might be useful shared here.

Popular Posts

Blogroll

Followers

Follow by Email