ある Arraylist データを別の arraylist に移動する方法。多くのオプションを試しましたが、出力はarraylistではなくarrayの形式です
25335 次
6 に答える
16
まず、.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
ます。もちろん、2番目のリストが十分に長いことがわかっている場合は、インデクサーをループできます。
for(int i = 0 ; i < bar.Count ; i++) {
foo[i] = bar[i];
}
新規作成するには:
List<int> bar = new List<int>(foo);
于 2009-02-09T09:46:29.807 に答える
6
ICollectionをパラメーターとして受け取るArrayListのコンストラクターを使用します。ほとんどのコレクションにはこのコンストラクターがあります。
ArrayList newList = new ArrayList(oldList);
于 2009-02-09T09:44:52.943 に答える
6
ArrayList model = new ArrayList();
ArrayList copy = new ArrayList(model);
?
于 2009-02-09T09:44:32.883 に答える
5
ArrayList l1=new ArrayList();
l1.Add("1");
l1.Add("2");
ArrayList l2=new ArrayList(l1);
于 2009-02-09T09:43:18.830 に答える
1
http://msdn.microsoft.com/en-us/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が注目を集めていると思います;)
于 2009-02-09T09:51:40.120 に答える
1
私は次のようにデータを上に移動するための答えを見つけました:
Firstarray.AddRange(SecondArrary);
于 2009-02-09T09:57:53.477 に答える