List<T>.GetRange(Int32, Int32) List<T>.GetRange(Int32, Int32) List<T>.GetRange(Int32, Int32) List<T>.GetRange(Int32, Int32) Method

定義

コピー元の List<T> 内の、ある範囲の要素の簡易コピーを作成します。Creates a shallow copy of a range of elements in the source List<T>.

public:
 System::Collections::Generic::List<T> ^ GetRange(int index, int count);
public System.Collections.Generic.List<T> GetRange (int index, int count);
member this.GetRange : int * int -> System.Collections.Generic.List<'T>
Public Function GetRange (index As Integer, count As Integer) As List(Of T)

パラメーター

index
Int32 Int32 Int32 Int32

範囲が開始する位置の、0 から始まる List<T> のインデックス番号。The zero-based List<T> index at which the range starts.

count
Int32 Int32 Int32 Int32

範囲内の要素の数。The number of elements in the range.

戻り値

コピー元の List<T> 内の、ある範囲の要素の簡易コピー。A shallow copy of a range of elements in the source List<T>.

例外

index が 0 未満です。index is less than 0.

または-or- count が 0 未満です。count is less than 0.

index および countList<T> において要素の有効な範囲を表していません。index and count do not denote a valid range of elements in the List<T>.

次の例で、GetRangeメソッドとその他の方法、List<T>範囲で動作するクラス。The following example demonstrates the GetRange method and other methods of the List<T> class that act on ranges. この例の最後に、GetRangeメソッドを使用して、インデックス位置 2 以降、リストから 3 つの項目を取得します。At the end of the example, the GetRange method is used to get three items from the list, beginning with index location 2. ToArrayメソッドは、結果としてList<T>、3 つの要素の配列を作成します。The ToArray method is called on the resulting List<T>, creating an array of three elements. 配列の要素が表示されます。The elements of the array are displayed.

using namespace System;
using namespace System::Collections::Generic;

void main()
{
    array<String^>^ input = { "Brachiosaurus", 
                              "Amargasaurus", 
                              "Mamenchisaurus" };

    List<String^>^ dinosaurs = 
        gcnew List<String^>((IEnumerable<String^>^) input);

    Console::WriteLine("\nCapacity: {0}", dinosaurs->Capacity);

    Console::WriteLine();
    for each(String^ dinosaur in dinosaurs )
    {
        Console::WriteLine(dinosaur);
    }

    Console::WriteLine("\nAddRange(dinosaurs)");
    dinosaurs->AddRange(dinosaurs);

    Console::WriteLine();
    for each(String^ dinosaur in dinosaurs )
    {
        Console::WriteLine(dinosaur);
    }

    Console::WriteLine("\nRemoveRange(2, 2)");
    dinosaurs->RemoveRange(2, 2);

    Console::WriteLine();
    for each(String^ dinosaur in dinosaurs )
    {
        Console::WriteLine(dinosaur);
    }

    input = gcnew array<String^> { "Tyrannosaurus", 
                                   "Deinonychus", 
                                   "Velociraptor"};

    Console::WriteLine("\nInsertRange(3, (IEnumerable<String^>^) input)");
    dinosaurs->InsertRange(3, (IEnumerable<String^>^) input);

    Console::WriteLine();
    for each(String^ dinosaur in dinosaurs )
    {
        Console::WriteLine(dinosaur);
    }

    Console::WriteLine("\noutput = dinosaurs->GetRange(2, 3)->ToArray()");
    array<String^>^ output = dinosaurs->GetRange(2, 3)->ToArray();
        
    Console::WriteLine();
    for each(String^ dinosaur in output )
    {
        Console::WriteLine(dinosaur);
    }
}

/* This code example produces the following output:

Capacity: 3

Brachiosaurus
Amargasaurus
Mamenchisaurus

AddRange(dinosaurs)

Brachiosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Amargasaurus
Mamenchisaurus

RemoveRange(2, 2)

Brachiosaurus
Amargasaurus
Amargasaurus
Mamenchisaurus

InsertRange(3, (IEnumerable<String^>^) input)

Brachiosaurus
Amargasaurus
Amargasaurus
Tyrannosaurus
Deinonychus
Velociraptor
Mamenchisaurus

output = dinosaurs->GetRange(2, 3)->ToArray()

Amargasaurus
Tyrannosaurus
Deinonychus
 */
using System;
using System.Collections.Generic;

public class Example
{
    public static void Main()
    {
        string[] input = { "Brachiosaurus", 
                           "Amargasaurus", 
                           "Mamenchisaurus" };

        List<string> dinosaurs = new List<string>(input);

        Console.WriteLine("\nCapacity: {0}", dinosaurs.Capacity);

        Console.WriteLine();
        foreach( string dinosaur in dinosaurs )
        {
            Console.WriteLine(dinosaur);
        }

        Console.WriteLine("\nAddRange(dinosaurs)");
        dinosaurs.AddRange(dinosaurs);

        Console.WriteLine();
        foreach( string dinosaur in dinosaurs )
        {
            Console.WriteLine(dinosaur);
        }

        Console.WriteLine("\nRemoveRange(2, 2)");
        dinosaurs.RemoveRange(2, 2);

        Console.WriteLine();
        foreach( string dinosaur in dinosaurs )
        {
            Console.WriteLine(dinosaur);
        }

        input = new string[] { "Tyrannosaurus", 
                               "Deinonychus", 
                               "Velociraptor"};

        Console.WriteLine("\nInsertRange(3, input)");
        dinosaurs.InsertRange(3, input);

        Console.WriteLine();
        foreach( string dinosaur in dinosaurs )
        {
            Console.WriteLine(dinosaur);
        }

        Console.WriteLine("\noutput = dinosaurs.GetRange(2, 3).ToArray()");
        string[] output = dinosaurs.GetRange(2, 3).ToArray();
        
        Console.WriteLine();
        foreach( string dinosaur in output )
        {
            Console.WriteLine(dinosaur);
        }
    }
}

/* This code example produces the following output:

Capacity: 3

Brachiosaurus
Amargasaurus
Mamenchisaurus

AddRange(dinosaurs)

Brachiosaurus
Amargasaurus
Mamenchisaurus
Brachiosaurus
Amargasaurus
Mamenchisaurus

RemoveRange(2, 2)

Brachiosaurus
Amargasaurus
Amargasaurus
Mamenchisaurus

InsertRange(3, input)

Brachiosaurus
Amargasaurus
Amargasaurus
Tyrannosaurus
Deinonychus
Velociraptor
Mamenchisaurus

output = dinosaurs.GetRange(2, 3).ToArray()

Amargasaurus
Tyrannosaurus
Deinonychus
 */
Imports System
Imports System.Collections.Generic

Public Class Example

    Public Shared Sub Main()

        Dim input() As String = { "Brachiosaurus", _
                                  "Amargasaurus", _
                                  "Mamenchisaurus" }

        Dim dinosaurs As New List(Of String)(input)

        Console.WriteLine(vbLf & "Capacity: {0}", dinosaurs.Capacity)

        Console.WriteLine()
        For Each dinosaur As String In dinosaurs
            Console.WriteLine(dinosaur)
        Next

        Console.WriteLine(vbLf & "AddRange(dinosaurs)")
        dinosaurs.AddRange(dinosaurs)

        Console.WriteLine()
        For Each dinosaur As String In dinosaurs
            Console.WriteLine(dinosaur)
        Next

        Console.WriteLine(vbLf & "RemoveRange(2, 2)")
        dinosaurs.RemoveRange(2, 2)

        Console.WriteLine()
        For Each dinosaur As String In dinosaurs
            Console.WriteLine(dinosaur)
        Next

        input = New String() { "Tyrannosaurus", _
                               "Deinonychus", _
                               "Velociraptor" }

        Console.WriteLine(vbLf & "InsertRange(3, input)")
        dinosaurs.InsertRange(3, input)

        Console.WriteLine()
        For Each dinosaur As String In dinosaurs
            Console.WriteLine(dinosaur)
        Next

        Console.WriteLine(vbLf & "output = dinosaurs.GetRange(2, 3).ToArray")
        Dim output() As String = dinosaurs.GetRange(2, 3).ToArray()
        
        Console.WriteLine()
        For Each dinosaur As String In output
            Console.WriteLine(dinosaur)
        Next

    End Sub
End Class

' This code example produces the following output:
'
'Capacity: 3
'
'Brachiosaurus
'Amargasaurus
'Mamenchisaurus
'
'AddRange(dinosaurs)
'
'Brachiosaurus
'Amargasaurus
'Mamenchisaurus
'Brachiosaurus
'Amargasaurus
'Mamenchisaurus
'
'RemoveRange(2, 2)
'
'Brachiosaurus
'Amargasaurus
'Amargasaurus
'Mamenchisaurus
'
'InsertRange(3, input)
'
'Brachiosaurus
'Amargasaurus
'Amargasaurus
'Tyrannosaurus
'Deinonychus
'Velociraptor
'Mamenchisaurus
'
'output = dinosaurs.GetRange(2, 3).ToArray
'
'Amargasaurus
'Tyrannosaurus
'Deinonychus

注釈

または、参照型のコレクション、そのコレクションのサブセットの簡易コピーには、コレクションの要素への参照のみが含まれています。A shallow copy of a collection of reference types, or a subset of that collection, contains only the references to the elements of the collection. オブジェクト自体はコピーされません。The objects themselves are not copied. 新しいリスト内の参照は、元のリスト内の参照と同じオブジェクトをポイントします。The references in the new list point to the same objects as the references in the original list.

値の型のコレクションまたは、そのコレクションのサブセットの簡易コピーには、コレクションの要素が含まれています。A shallow copy of a collection of value types, or a subset of that collection, contains the elements of the collection. ただし、コレクションの要素にその他のオブジェクトへの参照が含まれている場合、それらのオブジェクトはコピーされません。However, if the elements of the collection contain references to other objects, those objects are not copied. 新しいコレクションの要素内の参照は、元のコレクションの要素内の参照と同じオブジェクトをポイントします。The references in the elements of the new collection point to the same objects as the references in the elements of the original collection.

これに対し、コレクションのディープ コピーを要素をコピーして、要素が直接または間接的に参照するすべて。In contrast, a deep copy of a collection copies the elements and everything directly or indirectly referenced by the elements.

このメソッドは O (n) 操作、 ncountします。This method is an O(n) operation, where n is count.

適用対象

こちらもご覧ください