[英]Move one arraylist data to another arraylist in C#
如何將一個Arraylist數據移動到另一個arraylist。 我已經嘗試了很多選擇,但是輸出是數組而不是arraylist的形式
首先-除非您使用.NET 1.1,否則應避免使用ArrayList
首選類型化的集合,例如List<T>
。
當您說“復制”時-您要替換 , 追加還是創建新的 ?
對於追加(使用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);
要替換,添加一個foo.Clear();
在AddRange
之前。 當然,如果您知道第二個列表足夠長,則可以在索引器上循環:
for(int i = 0 ; i < bar.Count ; i++) {
foo[i] = bar[i];
}
創建新的:
List<int> bar = new List<int>(foo);
ArrayList model = new ArrayList();
ArrayList copy = new ArrayList(model);
?
使用以ICollection作為參數的ArrayList的構造函數。 大多數集合都具有此構造函數。
ArrayList newList = new ArrayList(oldList);
ArrayList l1=new ArrayList();
l1.Add("1");
l1.Add("2");
ArrayList l2=new ArrayList(l1);
http://msdn.microsoft.com/zh-CN/library/system.collections.arraylist.addrange.aspx
通過以上鏈接進行無恥的復制/粘貼
// 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' );
除此之外,我認為Marc Gravell很有意思 ;)
我找到了將數據向上移動的答案:
Firstarray.AddRange(SecondArrary);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.