List<T>.GetRange(Int32, Int32) 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
원본 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)
매개 변수
- count
- Int32
범위의 요소 수입니다.
반품
원본 List<T>에 있는 요소 범위의 단순 복사본입니다.
예외
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.