語言

List<T>.GetRange(Int32, Int32) 方法

定義

在來源 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

即區間起點的零基 List<T> 指數。

count
Int32

範圍中的元素數量。

傳回

源 中一系列元素 List<T>的淺層複製品。

例外狀況

index 小於0。

-或-

count 小於0。

index 且 count 不表示 中 List<T>有效元素範圍。

範例

以下範例展示了 GetRange 該類別中作用於範圍的方法及其他方法 List<T> 。 在範例的結尾,該方法用來 GetRange 從清單中取得三個項目,從索引位置 2 開始。 ToArray該方法在結果 List<T>上被呼叫,形成一個由三個元素組成的陣列。 陣列的元素會被顯示出來。

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

備註

一個淺層副本的參考類型集合,或該集合的子集,僅包含該集合元素的參考。 物件本身不會被複製。 新清單中的參考指向與原始清單中相同的物件。

一個值型別集合的淺層副本,或該集合的子集,包含該集合的元素。 然而,如果集合中的元素包含對其他物件的參考,則這些物件不會被複製。 新集合元素中的參考指向與原始集合元素中的參考相同的物件。

相較之下,深層副本則是直接或間接地複製元素及其所參考的一切。

此方法為 O(n) 運算,其中 n 為 count。

適用於

另請參閱