balaweblog
balaweblog

Reputation: 15460

Move one arraylist data to another arraylist in C#

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

Upvotes: 7

Views: 26599

Answers (6)

balaweblog
balaweblog

Reputation: 15460

I found the answer for moving up the data like :

Firstarray.AddRange(SecondArrary);

Upvotes: 1

thmsn
thmsn

Reputation: 1996

http://msdn.microsoft.com/en-us/library/system.collections.arraylist.addrange.aspx

shameless copy/paste from the above link

  // Creates and initializes a new ArrayList.
  ArrayList myAL = new ArrayList();
  myAL.Add( "The" );
  myAL.Add( "quick" );
  myAL.Add( "brown" );
  myAL.Add( "fox" );

  // Creates and initializes a new Queue.
  Queue myQueue = new Queue();
  myQueue.Enqueue( "jumped" );
  myQueue.Enqueue( "over" );
  myQueue.Enqueue( "the" );
  myQueue.Enqueue( "lazy" );
  myQueue.Enqueue( "dog" );

  // Displays the ArrayList and the Queue.
  Console.WriteLine( "The ArrayList initially contains the following:" );
  PrintValues( myAL, '\t' );
  Console.WriteLine( "The Queue initially contains the following:" );
  PrintValues( myQueue, '\t' );

  // Copies the Queue elements to the end of the ArrayList.
  myAL.AddRange( myQueue );

  // Displays the ArrayList.
  Console.WriteLine( "The ArrayList now contains the following:" );
  PrintValues( myAL, '\t' );

Other than that I think Marc Gravell is spot on ;)

Upvotes: 1

Marc Gravell
Marc Gravell

Reputation: 1062600

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);

Upvotes: 16

bang
bang

Reputation: 5221

Use the constructor of the ArrayList that takes an ICollection as a parameter. Most of the collections have this constructor.

ArrayList newList = new ArrayList(oldList);

Upvotes: 6

Konstantin Savelev
Konstantin Savelev

Reputation: 443

        ArrayList model = new ArrayList();
        ArrayList copy = new ArrayList(model);

?

Upvotes: 6

&#216;yvind Skaar
&#216;yvind Skaar

Reputation: 1840

ArrayList l1=new ArrayList();
l1.Add("1");
l1.Add("2");
ArrayList l2=new ArrayList(l1);

Upvotes: 5

Related Questions