7

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

1
  • Are you wanting a deep or shallow copy of the elements? Commented Feb 9, 2009 at 9:44

6 Answers 6

16

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);
Sign up to request clarification or add additional context in comments.

2 Comments

why did you recommend using List<T> instead of ArrayList unless using .NET 1.1???
@kashif type safety (avoiding stupid bugs), performance (boxing and memory), better API, support for LINQ, etc... Why wouldn't someone prefer the generic version? The only edge case I know about here is Hashtable, which still retains some usage because it has a much better threading model than Dictionary`2
6
        ArrayList model = new ArrayList();
        ArrayList copy = new ArrayList(model);

?

Comments

6

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

Comments

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

Comments

1

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

Comments

1

I found the answer for moving up the data like :

Firstarray.AddRange(SecondArrary);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.