在 C# 中将一个数组列表数据移动到另一个数组列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/527561/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Move one arraylist data to another arraylist in C#
提问by balaweblog
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
如何将一个 Arraylist 数据移动到另一个 arraylist。我尝试了很多选项,但输出的形式是数组而不是数组列表
回答by ?yvind Skaar
ArrayList l1=new ArrayList();
l1.Add("1");
l1.Add("2");
ArrayList l2=new ArrayList(l1);
回答by Konstantin Savelev
ArrayList model = new ArrayList();
ArrayList copy = new ArrayList(model);
?
?
回答by bang
Use the constructor of the ArrayList that takes an ICollection as a parameter. Most of the collections have this constructor.
使用将 ICollection 作为参数的 ArrayList 的构造函数。大多数集合都有这个构造函数。
ArrayList newList = new ArrayList(oldList);
回答by Marc Gravell
First - unless you are on .NET 1.1, you should a avoid ArrayList
- prefer typed collections such as List<T>
.
首先 - 除非您使用 .NET 1.1,否则您应该避免ArrayList
- 更喜欢类型化集合,例如List<T>
.
When you say "copy" - do you want to replace, append, or create new?
当您说“复制”时 - 您是要替换、追加还是创建新的?
For append (using 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);
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:
要替换,请foo.Clear();
在AddRange
. 当然,如果你知道第二个列表足够长,你可以在索引器上循环:
for(int i = 0 ; i < bar.Count ; i++) {
foo[i] = bar[i];
}
To create new:
要创建新的:
List<int> bar = new List<int>(foo);
回答by thmsn
http://msdn.microsoft.com/en-us/library/system.collections.arraylist.addrange.aspx
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 Gravellis spot on ;)
除此之外,我认为Marc Gravell是 ;)
回答by balaweblog
I found the answer for moving up the data like :
我找到了向上移动数据的答案,例如:
Firstarray.AddRange(SecondArrary);