Move data from one array to another arraylist in c #

How to transfer Arraylist data to another Arraist. I tried for many options, but the output is in the form of an array, not an arraylist

+7
arraylist c #
source share
6 answers

First - if you are not on .NET 1.1, you should avoid ArrayList - prefer typed collections like List<T> .

When you say โ€œcopy,โ€ do you want to replace, add, or create a new one?

To add (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 foo.Clear(); before AddRange . Of course, if you know that the second list is long enough, you can fixate on the indexer:

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

To create a new one:

  List<int> bar = new List<int>(foo); 
+15
source share
  ArrayList model = new ArrayList(); ArrayList copy = new ArrayList(model); 

?

+6
source share
 ArrayList l1=new ArrayList(); l1.Add("1"); l1.Add("2"); ArrayList l2=new ArrayList(l1); 
+4
source share

Use the ArrayList constructor, which takes an ICollection as a parameter. Most collections have this constructor.

 ArrayList newList = new ArrayList(oldList); 
+4
source share

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 Mark Gravell is on ;;

+1
source share

I found an answer for moving data like:

 Firstarray.AddRange(SecondArrary); 
0
source share

All Articles