SortedList.TrimToSize 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
용량을 SortedList 개체의 실제 요소 수로 설정합니다.
public:
virtual void TrimToSize();
public virtual void TrimToSize ();
abstract member TrimToSize : unit -> unit
override this.TrimToSize : unit -> unit
Public Overridable Sub TrimToSize ()
예외
예제
다음 코드 예제에서는 개체의 SortedList 사용되지 않는 부분을 자르는 방법과 해당 값을 지우는 방법을 보여 줍니다.
#using <system.dll>
using namespace System;
using namespace System::Collections;
void PrintKeysAndValues( SortedList^ myList )
{
Console::WriteLine( "\t-KEY-\t-VALUE-" );
for ( int i = 0; i < myList->Count; i++ )
{
Console::WriteLine( "\t{0}:\t{1}", myList->GetKey( i ), myList->GetByIndex( i ) );
}
Console::WriteLine();
}
int main()
{
// Creates and initializes a new SortedList.
SortedList^ mySL = gcnew SortedList;
mySL->Add( "one", "The" );
mySL->Add( "two", "quick" );
mySL->Add( "three", "brown" );
mySL->Add( "four", "fox" );
mySL->Add( "five", "jumps" );
// Displays the count, capacity and values of the SortedList.
Console::WriteLine( "Initially," );
Console::WriteLine( " Count : {0}", mySL->Count );
Console::WriteLine( " Capacity : {0}", mySL->Capacity );
Console::WriteLine( " Values:" );
PrintKeysAndValues( mySL );
// Trims the SortedList.
mySL->TrimToSize();
// Displays the count, capacity and values of the SortedList.
Console::WriteLine( "After TrimToSize," );
Console::WriteLine( " Count : {0}", mySL->Count );
Console::WriteLine( " Capacity : {0}", mySL->Capacity );
Console::WriteLine( " Values:" );
PrintKeysAndValues( mySL );
// Clears the SortedList.
mySL->Clear();
// Displays the count, capacity and values of the SortedList.
Console::WriteLine( "After Clear," );
Console::WriteLine( " Count : {0}", mySL->Count );
Console::WriteLine( " Capacity : {0}", mySL->Capacity );
Console::WriteLine( " Values:" );
PrintKeysAndValues( mySL );
// Trims the SortedList again.
mySL->TrimToSize();
// Displays the count, capacity and values of the SortedList.
Console::WriteLine( "After the second TrimToSize," );
Console::WriteLine( " Count : {0}", mySL->Count );
Console::WriteLine( " Capacity : {0}", mySL->Capacity );
Console::WriteLine( " Values:" );
PrintKeysAndValues( mySL );
}
/*
This code produces the following output.
Initially,
Count : 5
Capacity : 16
Values:
-KEY- -VALUE-
five: jumps
four: fox
one: The
three: brown
two: quick
After TrimToSize,
Count : 5
Capacity : 5
Values:
-KEY- -VALUE-
five: jumps
four: fox
one: The
three: brown
two: quick
After Clear,
Count : 0
Capacity : 16
Values:
-KEY- -VALUE-
After the second TrimToSize,
Count : 0
Capacity : 16
Values:
-KEY- -VALUE-
*/
using System;
using System.Collections;
public class SamplesSortedList {
public static void Main() {
// Creates and initializes a new SortedList.
SortedList mySL = new SortedList();
mySL.Add( "one", "The" );
mySL.Add( "two", "quick" );
mySL.Add( "three", "brown" );
mySL.Add( "four", "fox" );
mySL.Add( "five", "jumps" );
// Displays the count, capacity and values of the SortedList.
Console.WriteLine( "Initially," );
Console.WriteLine( " Count : {0}", mySL.Count );
Console.WriteLine( " Capacity : {0}", mySL.Capacity );
Console.WriteLine( " Values:" );
PrintKeysAndValues( mySL );
// Trims the SortedList.
mySL.TrimToSize();
// Displays the count, capacity and values of the SortedList.
Console.WriteLine( "After TrimToSize," );
Console.WriteLine( " Count : {0}", mySL.Count );
Console.WriteLine( " Capacity : {0}", mySL.Capacity );
Console.WriteLine( " Values:" );
PrintKeysAndValues( mySL );
// Clears the SortedList.
mySL.Clear();
// Displays the count, capacity and values of the SortedList.
Console.WriteLine( "After Clear," );
Console.WriteLine( " Count : {0}", mySL.Count );
Console.WriteLine( " Capacity : {0}", mySL.Capacity );
Console.WriteLine( " Values:" );
PrintKeysAndValues( mySL );
// Trims the SortedList again.
mySL.TrimToSize();
// Displays the count, capacity and values of the SortedList.
Console.WriteLine( "After the second TrimToSize," );
Console.WriteLine( " Count : {0}", mySL.Count );
Console.WriteLine( " Capacity : {0}", mySL.Capacity );
Console.WriteLine( " Values:" );
PrintKeysAndValues( mySL );
}
public static void PrintKeysAndValues( SortedList myList ) {
Console.WriteLine( "\t-KEY-\t-VALUE-" );
for ( int i = 0; i < myList.Count; i++ ) {
Console.WriteLine( "\t{0}:\t{1}", myList.GetKey(i), myList.GetByIndex(i) );
}
Console.WriteLine();
}
}
/*
This code produces the following output.
Initially,
Count : 5
Capacity : 16
Values:
-KEY- -VALUE-
five: jumps
four: fox
one: The
three: brown
two: quick
After TrimToSize,
Count : 5
Capacity : 5
Values:
-KEY- -VALUE-
five: jumps
four: fox
one: The
three: brown
two: quick
After Clear,
Count : 0
Capacity : 16
Values:
-KEY- -VALUE-
After the second TrimToSize,
Count : 0
Capacity : 16
Values:
-KEY- -VALUE-
*/
Imports System.Collections
Public Class SamplesSortedList
Public Shared Sub Main()
' Creates and initializes a new SortedList.
Dim mySL As New SortedList()
mySL.Add("one", "The")
mySL.Add("two", "quick")
mySL.Add("three", "brown")
mySL.Add("four", "fox")
mySL.Add("five", "jumps")
' Displays the count, capacity and values of the SortedList.
Console.WriteLine("Initially,")
Console.WriteLine(" Count : {0}", mySL.Count)
Console.WriteLine(" Capacity : {0}", mySL.Capacity)
Console.WriteLine(" Values:")
PrintKeysAndValues(mySL)
' Trims the SortedList.
mySL.TrimToSize()
' Displays the count, capacity and values of the SortedList.
Console.WriteLine("After TrimToSize,")
Console.WriteLine(" Count : {0}", mySL.Count)
Console.WriteLine(" Capacity : {0}", mySL.Capacity)
Console.WriteLine(" Values:")
PrintKeysAndValues(mySL)
' Clears the SortedList.
mySL.Clear()
' Displays the count, capacity and values of the SortedList.
Console.WriteLine("After Clear,")
Console.WriteLine(" Count : {0}", mySL.Count)
Console.WriteLine(" Capacity : {0}", mySL.Capacity)
Console.WriteLine(" Values:")
PrintKeysAndValues(mySL)
' Trims the SortedList again.
mySL.TrimToSize()
' Displays the count, capacity and values of the SortedList.
Console.WriteLine("After the second TrimToSize,")
Console.WriteLine(" Count : {0}", mySL.Count)
Console.WriteLine(" Capacity : {0}", mySL.Capacity)
Console.WriteLine(" Values:")
PrintKeysAndValues(mySL)
End Sub
Public Shared Sub PrintKeysAndValues(myList As SortedList)
Console.WriteLine(ControlChars.Tab & "-KEY-" & ControlChars.Tab & _
"-VALUE-")
Dim i As Integer
For i = 0 To myList.Count - 1
Console.WriteLine(ControlChars.Tab & "{0}:" & ControlChars.Tab & _
"{1}", myList.GetKey(i), myList.GetByIndex(i))
Next i
Console.WriteLine()
End Sub
End Class
' This code produces the following output.
'
' Initially,
' Count : 5
' Capacity : 16
' Values:
' -KEY- -VALUE-
' five: jumps
' four: fox
' one: The
' three: brown
' two: quick
'
' After TrimToSize,
' Count : 5
' Capacity : 5
' Values:
' -KEY- -VALUE-
' five: jumps
' four: fox
' one: The
' three: brown
' two: quick
'
' After Clear,
' Count : 0
' Capacity : 16
' Values:
' -KEY- -VALUE-
'
'
' After the second TrimToSize,
' Count : 0
' Capacity : 16
' Values:
' -KEY- -VALUE-
설명
컬렉션에 새 요소가 추가되지 않는 경우 이 메서드를 사용하여 컬렉션의 메모리 오버헤드를 최소화할 수 있습니다.
개체를 SortedList 초기 상태로 다시 설정하려면 를 호출하기 전에 메서드를 Clear 호출 TrimToSize합니다. 빈 SortedList 값을 트리밍하면 의 SortedList 용량이 기본 용량으로 설정됩니다.
이 메서드는 작업입니다 O(n)
. 여기서 n
는 입니다 Count.
적용 대상
추가 정보
GitHub에서 Microsoft와 공동 작업
이 콘텐츠의 원본은 GitHub에서 찾을 수 있으며, 여기서 문제와 끌어오기 요청을 만들고 검토할 수도 있습니다. 자세한 내용은 참여자 가이드를 참조하세요.
.NET