Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Move one arraylist data to another arraylist in C#

Tags:

c#

arraylist

How to move one Arraylist data to another arraylist. I have tried for many option but the output is in the form of array not arraylist

like image 446
balaweblog Avatar asked Feb 09 '09 09:02

balaweblog


People also ask

How do I move data from one ArrayList to another?

In order to copy elements of ArrayList to another ArrayList, we use the Collections. copy() method. It is used to copy all elements of a collection into another. where src is the source list object and dest is the destination list object.

Can you set an ArrayList equal to another ArrayList?

The clone() method of the ArrayList class is used to clone an ArrayList to another ArrayList in Java as it returns a shallow copy of its caller ArrayList.

How do you make two ArrayList equal in Java?

The ArrayList. equals() is the method used for comparing two Array List. It compares the Array lists as, both Array lists should have the same size, and all corresponding pairs of elements in the two Array lists are equal. Parameters: This function has a single parameter which is an object to be compared for equality.


1 Answers

First - unless you are on .NET 1.1, you should a avoid ArrayList - prefer typed collections such as List<T>.

When you say "copy" - do you want to replace, append, or create new?

For append (using List<T>):

    List<int> foo = new List<int> { 1, 2, 3, 4, 5 };
    List<int> bar = new List<int> { 6, 7, 8, 9, 10 };
    foo.AddRange(bar);

To replace, add a foo.Clear(); before the AddRange. Of course, if you know the second list is long enough, you could loop on the indexer:

    for(int i = 0 ; i < bar.Count ; i++) {
        foo[i] = bar[i];
    }

To create new:

    List<int> bar = new List<int>(foo);
like image 154
Marc Gravell Avatar answered Dec 07 '22 05:12

Marc Gravell