2009-02-09 22 views

Respuesta

4
ArrayList l1=new ArrayList(); 
l1.Add("1"); 
l1.Add("2"); 
ArrayList l2=new ArrayList(l1); 
6
 ArrayList model = new ArrayList(); 
     ArrayList copy = new ArrayList(model); 

?

+0

Esta es la respuesta correcta. – Matth3w

4

Utilice el constructor de ArrayList que toma una ICollection como parámetro. La mayoría de las colecciones tienen este constructor.

ArrayList newList = new ArrayList(oldList); 
15

En primer lugar - a menos que esté en .NET 1.1, se debe evitar una ArrayList - prefiere colecciones escrito como List<T>.

Cuando dice "copia" - ¿desea reemplazar , anexar, o crear nuevas ?

Por adición (usando 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); 

Para reemplazar, añadir un foo.Clear(); antes de la AddRange. Por supuesto, si conoce la segunda lista es lo suficientemente largo, puede bucle en el indexador:

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

para crear nuevas:

List<int> bar = new List<int>(foo); 
+1

¿por qué recomienda usar la Lista en lugar de ArrayList a menos que use .NET 1.1 ??? – kashif

+3

@kashif tipo de seguridad (evitando errores estúpidos), rendimiento (boxeo y memoria), mejor API, soporte para LINQ, etc ... ¿Por qué * no * alguien prefiere la versión genérica? El único caso límite que conozco aquí es Hashtable, que todavía conserva algo de uso porque tiene un modelo de subprocesamiento mucho mejor que el Dictionary'2 –

1

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

copia descarada/pegar de lo anterior 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'); 

Aparte de eso creo que Marc Gravell es perfecto ;)

+0

@Konstantin Savelev tenía razón – Matth3w

0

he encontrado la respuesta para mover hacia arriba los datos como:

Firstarray.AddRange(SecondArrary); 
Cuestiones relacionadas