List<T>.InsertRange(Int32, IEnumerable<T>) Metoda
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Vloží prvky kolekce do zadaného indexu List<T> .
public:
void InsertRange(int index, System::Collections::Generic::IEnumerable<T> ^ collection);
public void InsertRange(int index, System.Collections.Generic.IEnumerable<T> collection);
member this.InsertRange : int * seq<'T> -> unit
Public Sub InsertRange (index As Integer, collection As IEnumerable(Of T))
Parametry
- index
- Int32
Index založený na nule, do kterého by měly být vloženy nové prvky.
- collection
- IEnumerable<T>
Kolekce, jejíž prvky by měly být vloženy List<T>do . Samotná kolekce nemůže být null, ale může obsahovat prvky, které jsou null, pokud typ T je typ odkaz.
Výjimky
collection je null.
Příklady
Následující příklad ukazuje InsertRange metodu a různé jiné metody List<T> třídy, které působí na rozsahy. Poté, co byl seznam vytvořen a naplněn názvy několika klidných rostlin jíst dinosaury, InsertRange metoda se používá k vložení pole tří divokých maso-jíst dinosaurů do seznamu, počínaje indexem umístění 3.
using System;
using System.Collections.Generic;
string[] input = { "Apple",
"Banana",
"Orange" };
List<string> fruits = new List<string>(input);
Console.WriteLine("\nCapacity: {0}", fruits.Capacity);
Console.WriteLine();
foreach (string fruit in fruits)
{
Console.WriteLine(fruit);
}
Console.WriteLine("\nAddRange(fruits)");
fruits.AddRange(fruits);
Console.WriteLine();
foreach (string fruit in fruits)
{
Console.WriteLine(fruit);
}
Console.WriteLine("\nRemoveRange(2, 2)");
fruits.RemoveRange(2, 2);
Console.WriteLine();
foreach (string fruit in fruits)
{
Console.WriteLine(fruit);
}
input = new string[] { "Mango",
"Pineapple",
"Watermelon" };
Console.WriteLine("\nInsertRange(3, input)");
fruits.InsertRange(3, input);
Console.WriteLine();
foreach (string fruit in fruits)
{
Console.WriteLine(fruit);
}
Console.WriteLine("\noutput = fruits.GetRange(2, 3).ToArray()");
string[] output = fruits.GetRange(2, 3).ToArray();
Console.WriteLine();
foreach (string fruit in output)
{
Console.WriteLine(fruit);
}
/*
This code example produces the following output:
Capacity: 3
Apple
Banana
Orange
AddRange(fruits)
Apple
Banana
Orange
Apple
Banana
Orange
RemoveRange(2, 2)
Apple
Banana
Banana
Orange
InsertRange(3, input)
Apple
Banana
Banana
Mango
Pineapple
Watermelon
Orange
output = fruits.GetRange(2, 3).ToArray()
Banana
Mango
Pineapple
*/
Imports System.Collections.Generic
Partial Public Class Program
Public Shared Sub ShowFruits()
Dim input() As String = { "Apple", _
"Banana", _
"Orange" }
Dim fruits As New List(Of String)(input)
Console.WriteLine(vbLf & "Capacity: {0}", fruits.Capacity)
Console.WriteLine()
For Each fruit As String In fruits
Console.WriteLine(fruit)
Next
Console.WriteLine(vbLf & "AddRange(fruits)")
fruits.AddRange(fruits)
Console.WriteLine()
For Each fruit As String In fruits
Console.WriteLine(fruit)
Next
Console.WriteLine(vbLf & "RemoveRange(2, 2)")
fruits.RemoveRange(2, 2)
Console.WriteLine()
For Each fruit As String In fruits
Console.WriteLine(fruit)
Next
input = New String() { "Mango", _
"Pineapple", _
"Watermelon" }
Console.WriteLine(vbLf & "InsertRange(3, input)")
fruits.InsertRange(3, input)
Console.WriteLine()
For Each fruit As String In fruits
Console.WriteLine(fruit)
Next
Console.WriteLine(vbLf & "output = fruits.GetRange(2, 3).ToArray")
Dim output() As String = fruits.GetRange(2, 3).ToArray()
Console.WriteLine()
For Each fruit As String In output
Console.WriteLine(fruit)
Next
End Sub
End Class
' This code example produces the following output:
'
' Capacity: 3
'
' Apple
' Banana
' Orange
'
' AddRange(fruits)
'
' Apple
' Banana
' Orange
' Apple
' Banana
' Orange
'
' RemoveRange(2, 2)
'
' Apple
' Banana
' Banana
' Orange
'
' InsertRange(3, input)
'
' Apple
' Banana
' Banana
' Mango
' Pineapple
' Watermelon
' Orange
'
' output = fruits.GetRange(2, 3).ToArray
'
' Banana
' Mango
' Pineapple
Poznámky
List<T> přijímá null jako platnou hodnotu pro odkazové typy a umožňuje duplicitní prvky.
Pokud bude nová Count (aktuální Count plus velikost kolekce) větší než Capacity, zvýší se kapacita objektu List<T> automaticky přidělením interního pole tak, aby vyhovovala novým prvkům, a existující prvky se zkopírují do nového pole před přidáním nových prvků.
Je-li index roven Count, prvky jsou přidány na konec .List<T>
Pořadí prvků v kolekci je zachováno List<T>v souboru .
Tato metoda je operace O(n * m), kde n je počet prvků, které mají být přidány a m je Count.