两个ArrayList 如何合成一个
每个ArrayList中包含着若干个对象,如何将两个合成一个ArrayList呢? --------------------编程问答-------------------- ArrayList1.AddRange(ArrayList2); --------------------编程问答-------------------- 贴段MSDN的例子using System;
using System.Collections;
public class SamplesArrayList {
public static void Main() {
// 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' );
}
public static void PrintValues( IEnumerable myList, char mySeparator ) {
foreach ( Object obj in myList )
Console.Write( "{0}{1}", mySeparator, obj );
Console.WriteLine();
}
}
/*
This code produces the following output.
The ArrayList initially contains the following:
The quick brown fox
The Queue initially contains the following:
jumped over the lazy dog
The ArrayList now contains the following:
The quick brown fox jumped over the lazy dog
*/ --------------------编程问答--------------------
同意 --------------------编程问答-------------------- ArrayList1.AddRange(ArrayList2); --------------------编程问答-------------------- 一楼正解 --------------------编程问答-------------------- --------------------编程问答-------------------- ArrayList1.AddRange(ArrayList2); 顶一楼的! --------------------编程问答-------------------- 推荐改用泛型,同样有AddRange方法。效率远比ArrayList高。
补充:.NET技术 , C#