SortedList<TKey,TValue> Constructors
Definitie
Belangrijk
Bepaalde informatie heeft betrekking op een voorlopige productversie die aanzienlijk kan worden gewijzigd voordat deze wordt uitgebracht. Microsoft biedt geen enkele expliciete of impliciete garanties met betrekking tot de informatie die hier wordt verstrekt.
Initialiseert een nieuw exemplaar van de SortedList<TKey,TValue> klasse.
Overloads
| Name | Description |
|---|---|
| SortedList<TKey,TValue>() |
Initialiseert een nieuw exemplaar van de SortedList<TKey,TValue> klasse die leeg is, heeft de standaardinitiële capaciteit en gebruikt de standaardwaarde IComparer<T>. |
| SortedList<TKey,TValue>(IComparer<TKey>) |
Initialiseert een nieuw exemplaar van de SortedList<TKey,TValue> klasse die leeg is, heeft de standaardinitiële capaciteit en gebruikt de opgegeven IComparer<T>. |
| SortedList<TKey,TValue>(IDictionary<TKey,TValue>) |
Initialiseert een nieuwe instantie van de SortedList<TKey,TValue> klasse die elementen bevat die zijn gekopieerd uit de opgegeven IDictionary<TKey,TValue>, heeft voldoende capaciteit om het aantal gekopieerde elementen te kunnen opvangen en gebruikt de standaardwaarde IComparer<T>. |
| SortedList<TKey,TValue>(Int32) |
Initialiseert een nieuw exemplaar van de SortedList<TKey,TValue> klasse die leeg is, heeft de opgegeven initiële capaciteit en gebruikt de standaardwaarde IComparer<T>. |
| SortedList<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>) |
Initialiseert een nieuw exemplaar van de SortedList<TKey,TValue> klasse die elementen bevat die zijn gekopieerd uit de opgegeven IDictionary<TKey,TValue>, heeft voldoende capaciteit om het aantal gekopieerde elementen te kunnen opvangen en gebruikt de opgegeven IComparer<T>. |
| SortedList<TKey,TValue>(Int32, IComparer<TKey>) |
Initialiseert een nieuw exemplaar van de SortedList<TKey,TValue> klasse die leeg is, heeft de opgegeven initiële capaciteit en gebruikt de opgegeven IComparer<T>. |
SortedList<TKey,TValue>()
Initialiseert een nieuw exemplaar van de SortedList<TKey,TValue> klasse die leeg is, heeft de standaardinitiële capaciteit en gebruikt de standaardwaarde IComparer<T>.
public:
SortedList();
public SortedList();
Public Sub New ()
Voorbeelden
In het volgende codevoorbeeld wordt een lege SortedList<TKey,TValue> tekenreeks met tekenreekssleutels gemaakt en wordt de Add methode gebruikt om enkele elementen toe te voegen. In het voorbeeld ziet u dat de Add methode een ArgumentException dubbele sleutel genereert bij het toevoegen van een dubbele sleutel.
Dit codevoorbeeld maakt deel uit van een groter voorbeeld voor de SortedList<TKey,TValue> klasse.
// Create a new sorted list of strings, with string
// keys.
SortedList<string, string> openWith =
new SortedList<string, string>();
// Add some elements to the list. There are no
// duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");
// The Add method throws an exception if the new key is
// already in the list.
try
{
openWith.Add("txt", "winword.exe");
}
catch (ArgumentException)
{
Console.WriteLine("An element with Key = \"txt\" already exists.");
}
' Create a new sorted list of strings, with string
' keys.
Dim openWith As New SortedList(Of String, String)
' Add some elements to the list. There are no
' duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe")
openWith.Add("bmp", "paint.exe")
openWith.Add("dib", "paint.exe")
openWith.Add("rtf", "wordpad.exe")
' The Add method throws an exception if the new key is
' already in the list.
Try
openWith.Add("txt", "winword.exe")
Catch
Console.WriteLine("An element with Key = ""txt"" already exists.")
End Try
// Create a new sorted list of strings, with string
// keys.
let openWith = SortedList<string, string>()
// Add some elements to the list. There are no
// duplicate keys, but some of the values are duplicates.
openWith.Add("txt", "notepad.exe")
openWith.Add("bmp", "paint.exe")
openWith.Add("dib", "paint.exe")
openWith.Add("rtf", "wordpad.exe")
// The Add method throws an exception if the new key is
// already in the list.
try
openWith.Add("txt", "winword.exe");
with
| :? ArgumentException ->
printfn "An element with Key = \"txt\" already exists."
Opmerkingen
Elke sleutel in een SortedList<TKey,TValue> moet uniek zijn volgens de standaard comparer.
Deze constructor gebruikt de standaardwaarde voor de initiële capaciteit van de SortedList<TKey,TValue>. Gebruik de SortedList<TKey,TValue>(Int32) constructor om de initiële capaciteit in te stellen. Als de uiteindelijke grootte van de verzameling kan worden geschat, is het opgeven van de initiële capaciteit niet meer nodig om een aantal groottebewerkingen uit te voeren terwijl er elementen aan de SortedList<TKey,TValue>verzameling worden toegevoegd.
Deze constructor maakt gebruik van de standaard comparer voor TKey. Als u een vergelijking wilt opgeven, gebruikt u de SortedList<TKey,TValue>(IComparer<TKey>) constructor. De standaard comparer Comparer<T>.Default controleert of het sleuteltype TKey die implementatie implementeert System.IComparable<T> en gebruikt, indien beschikbaar. Zo niet, Comparer<T>.Default controleert u of het sleuteltype TKey wordt geïmplementeerd System.IComparable. Als het sleuteltype TKey geen van beide interfaces implementeert, kunt u een System.Collections.Generic.IComparer<T> implementatie opgeven in een overbelasting van een constructor die een comparer parameter accepteert.
Deze constructor is een O(1)-bewerking.
Zie ook
Van toepassing op
SortedList<TKey,TValue>(IComparer<TKey>)
Initialiseert een nieuw exemplaar van de SortedList<TKey,TValue> klasse die leeg is, heeft de standaardinitiële capaciteit en gebruikt de opgegeven IComparer<T>.
public:
SortedList(System::Collections::Generic::IComparer<TKey> ^ comparer);
public SortedList(System.Collections.Generic.IComparer<TKey> comparer);
new System.Collections.Generic.SortedList<'Key, 'Value> : System.Collections.Generic.IComparer<'Key> -> System.Collections.Generic.SortedList<'Key, 'Value>
Public Sub New (comparer As IComparer(Of TKey))
Parameters
- comparer
- IComparer<TKey>
De IComparer<T> implementatie die moet worden gebruikt bij het vergelijken van sleutels.
– of –
null als u de standaardwaarde Comparer<T> wilt gebruiken voor het type sleutel.
Voorbeelden
In het volgende codevoorbeeld wordt een gesorteerde lijst gemaakt met een hoofdlettergevoelige vergelijking voor de huidige cultuur. In het voorbeeld worden vier elementen toegevoegd, sommige met kleine letters en sommige met hoofdletters. Het voorbeeld probeert vervolgens een element toe te voegen met een sleutel die alleen per geval verschilt van een bestaande sleutel, de resulterende uitzondering onderschept en een foutbericht weergeeft. Ten slotte geeft het voorbeeld de elementen weer in hoofdlettergevoelige sorteervolgorde.
using System;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
// Create a new sorted list of strings, with string keys and
// a case-insensitive comparer for the current culture.
SortedList<string, string> openWith =
new SortedList<string, string>(
StringComparer.CurrentCultureIgnoreCase);
// Add some elements to the list.
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("DIB", "paint.exe");
openWith.Add("rtf", "wordpad.exe");
// Try to add a fifth element with a key that is the same
// except for case; this would be allowed with the default
// comparer.
try
{
openWith.Add("BMP", "paint.exe");
}
catch (ArgumentException)
{
Console.WriteLine("\nBMP is already in the sorted list.");
}
// List the contents of the sorted list.
Console.WriteLine();
foreach( KeyValuePair<string, string> kvp in openWith )
{
Console.WriteLine("Key = {0}, Value = {1}", kvp.Key,
kvp.Value);
}
}
}
/* This code example produces the following output:
BMP is already in the sorted list.
Key = bmp, Value = paint.exe
Key = DIB, Value = paint.exe
Key = rtf, Value = wordpad.exe
Key = txt, Value = notepad.exe
*/
Imports System.Collections.Generic
Public Class Example
Public Shared Sub Main()
' Create a new sorted list of strings, with string keys and
' a case-insensitive comparer for the current culture.
Dim openWith As New SortedList(Of String, String)( _
StringComparer.CurrentCultureIgnoreCase)
' Add some elements to the list.
openWith.Add("txt", "notepad.exe")
openWith.Add("bmp", "paint.exe")
openWith.Add("DIB", "paint.exe")
openWith.Add("rtf", "wordpad.exe")
' Try to add a fifth element with a key that is the same
' except for case; this would be allowed with the default
' comparer.
Try
openWith.Add("BMP", "paint.exe")
Catch ex As ArgumentException
Console.WriteLine(vbLf & "BMP is already in the sorted list.")
End Try
' List the contents of the sorted list.
Console.WriteLine()
For Each kvp As KeyValuePair(Of String, String) In openWith
Console.WriteLine("Key = {0}, Value = {1}", _
kvp.Key, kvp.Value)
Next kvp
End Sub
End Class
' This code example produces the following output:
'
'BMP is already in the sorted list.
'
'Key = bmp, Value = paint.exe
'Key = DIB, Value = paint.exe
'Key = rtf, Value = wordpad.exe
'Key = txt, Value = notepad.exe
Opmerkingen
Elke sleutel in een SortedList<TKey,TValue> moet uniek zijn volgens de opgegeven vergelijkingsfunctie.
Deze constructor gebruikt de standaardwaarde voor de initiële capaciteit van de SortedList<TKey,TValue>. Gebruik de SortedList<TKey,TValue>(Int32, IComparer<TKey>) constructor om de initiële capaciteit in te stellen. Als de uiteindelijke grootte van de verzameling kan worden geschat, is het opgeven van de initiële capaciteit niet meer nodig om een aantal groottebewerkingen uit te voeren terwijl er elementen aan de SortedList<TKey,TValue>verzameling worden toegevoegd.
Deze constructor is een O(1)-bewerking.
Zie ook
Van toepassing op
SortedList<TKey,TValue>(IDictionary<TKey,TValue>)
Initialiseert een nieuwe instantie van de SortedList<TKey,TValue> klasse die elementen bevat die zijn gekopieerd uit de opgegeven IDictionary<TKey,TValue>, heeft voldoende capaciteit om het aantal gekopieerde elementen te kunnen opvangen en gebruikt de standaardwaarde IComparer<T>.
public:
SortedList(System::Collections::Generic::IDictionary<TKey, TValue> ^ dictionary);
public SortedList(System.Collections.Generic.IDictionary<TKey,TValue> dictionary);
new System.Collections.Generic.SortedList<'Key, 'Value> : System.Collections.Generic.IDictionary<'Key, 'Value> -> System.Collections.Generic.SortedList<'Key, 'Value>
Public Sub New (dictionary As IDictionary(Of TKey, TValue))
Parameters
- dictionary
- IDictionary<TKey,TValue>
De IDictionary<TKey,TValue> elementen waarvan de elementen naar het nieuwe SortedList<TKey,TValue>worden gekopieerd.
Uitzonderingen
dictionary is null.
dictionary bevat een of meer dubbele sleutels.
Voorbeelden
In het volgende codevoorbeeld ziet u hoe SortedList<TKey,TValue> u een gesorteerde kopie van de informatie in een Dictionary<TKey,TValue>maakt door de Dictionary<TKey,TValue> constructor door te geven aan de SortedList<TKey,TValue>(IDictionary<TKey,TValue>) constructor.
using System;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
// Create a new Dictionary of strings, with string keys.
//
Dictionary<string, string> openWith =
new Dictionary<string, string>();
// Add some elements to the dictionary.
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");
// Create a SortedList of strings with string keys,
// and initialize it with the contents of the Dictionary.
SortedList<string, string> copy =
new SortedList<string, string>(openWith);
// List the contents of the copy.
Console.WriteLine();
foreach( KeyValuePair<string, string> kvp in copy )
{
Console.WriteLine("Key = {0}, Value = {1}",
kvp.Key, kvp.Value);
}
}
}
/* This code example produces the following output:
Key = bmp, Value = paint.exe
Key = dib, Value = paint.exe
Key = rtf, Value = wordpad.exe
Key = txt, Value = notepad.exe
*/
Imports System.Collections.Generic
Public Class Example
Public Shared Sub Main()
' Create a new Dictionary of strings, with string
' keys.
Dim openWith As New Dictionary(Of String, String)
' Add some elements to the dictionary.
openWith.Add("txt", "notepad.exe")
openWith.Add("bmp", "paint.exe")
openWith.Add("dib", "paint.exe")
openWith.Add("rtf", "wordpad.exe")
' Create a SortedList of strings with string keys,
' and initialize it with the contents of the Dictionary.
Dim copy As New SortedList(Of String, String)(openWith)
' List the sorted contents of the copy.
Console.WriteLine()
For Each kvp As KeyValuePair(Of String, String) In copy
Console.WriteLine("Key = {0}, Value = {1}", _
kvp.Key, kvp.Value)
Next kvp
End Sub
End Class
' This code example produces the following output:
'
'Key = bmp, Value = paint.exe
'Key = dib, Value = paint.exe
'Key = rtf, Value = wordpad.exe
'Key = txt, Value = notepad.exe
Opmerkingen
Elke sleutel in een SortedList<TKey,TValue> moet uniek zijn op basis van de standaard comparer. Op dezelfde manier moet elke sleutel in de bron dictionary ook uniek zijn volgens de standaard-vergelijkingsfunctie.
De capaciteit van het nieuwe SortedList<TKey,TValue> wordt ingesteld op het aantal elementen in dictionary, dus er vindt geen formaat wijzigen plaats terwijl de lijst wordt gevuld.
Deze constructor maakt gebruik van de standaard comparer voor TKey. Als u een vergelijking wilt opgeven, gebruikt u de SortedList<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>) constructor. De standaard comparer Comparer<T>.Default controleert of het sleuteltype TKey die implementatie implementeert System.IComparable<T> en gebruikt, indien beschikbaar. Zo niet, Comparer<T>.Default controleert u of het sleuteltype TKey wordt geïmplementeerd System.IComparable. Als het sleuteltype TKey geen van beide interfaces implementeert, kunt u een System.Collections.Generic.IComparer<T> implementatie opgeven in een overbelasting van een constructor die een comparer parameter accepteert.
De sleutels dictionary worden één keer gekopieerd naar de nieuwe SortedList<TKey,TValue> en gesorteerd, waardoor deze constructor een O(n logboek n) bewerking.
Zie ook
Van toepassing op
SortedList<TKey,TValue>(Int32)
Initialiseert een nieuw exemplaar van de SortedList<TKey,TValue> klasse die leeg is, heeft de opgegeven initiële capaciteit en gebruikt de standaardwaarde IComparer<T>.
public:
SortedList(int capacity);
public SortedList(int capacity);
new System.Collections.Generic.SortedList<'Key, 'Value> : int -> System.Collections.Generic.SortedList<'Key, 'Value>
Public Sub New (capacity As Integer)
Parameters
- capacity
- Int32
Het eerste aantal elementen dat de SortedList<TKey,TValue> elementen kunnen bevatten.
Uitzonderingen
capacity is kleiner dan nul.
Voorbeelden
In het volgende codevoorbeeld wordt een gesorteerde lijst gemaakt met een initiële capaciteit van 4 en wordt deze gevuld met vier vermeldingen.
using System;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
// Create a new sorted list of strings, with string keys and
// an initial capacity of 4.
SortedList<string, string> openWith =
new SortedList<string, string>(4);
// Add 4 elements to the list.
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("dib", "paint.exe");
openWith.Add("rtf", "wordpad.exe");
// List the contents of the sorted list.
Console.WriteLine();
foreach( KeyValuePair<string, string> kvp in openWith )
{
Console.WriteLine("Key = {0}, Value = {1}",
kvp.Key, kvp.Value);
}
}
}
/* This code example produces the following output:
Key = bmp, Value = paint.exe
Key = dib, Value = paint.exe
Key = rtf, Value = wordpad.exe
Key = txt, Value = notepad.exe
*/
Imports System.Collections.Generic
Public Class Example
Public Shared Sub Main()
' Create a new sorted list of strings, with string keys and
' an initial capacity of 4.
Dim openWith As New SortedList(Of String, String)(4)
' Add 4 elements to the list.
openWith.Add("txt", "notepad.exe")
openWith.Add("bmp", "paint.exe")
openWith.Add("dib", "paint.exe")
openWith.Add("rtf", "wordpad.exe")
' List the contents of the sorted list.
Console.WriteLine()
For Each kvp As KeyValuePair(Of String, String) In openWith
Console.WriteLine("Key = {0}, Value = {1}", _
kvp.Key, kvp.Value)
Next kvp
End Sub
End Class
' This code example produces the following output:
'
'Key = bmp, Value = paint.exe
'Key = dib, Value = paint.exe
'Key = rtf, Value = wordpad.exe
'Key = txt, Value = notepad.exe
Opmerkingen
Elke sleutel in een SortedList<TKey,TValue> moet uniek zijn volgens de standaard comparer.
De capaciteit van een SortedList<TKey,TValue> is het aantal elementen dat kan worden vastgeslagen voordat u het SortedList<TKey,TValue> formaat wijzigt. Als er elementen aan een SortedList<TKey,TValue>worden toegevoegd, wordt de capaciteit automatisch verhoogd zoals vereist door de interne matrix opnieuw te verplaatsen.
Als de grootte van de verzameling kan worden geschat, hoeft u bij het opgeven van de initiële capaciteit niet meer een aantal groottebewerkingen uit te voeren terwijl er elementen aan de SortedList<TKey,TValue>verzameling worden toegevoegd.
De capaciteit kan worden verlaagd door aan te roepen TrimExcess of door de Capacity eigenschap expliciet in te stellen. Als u de capaciteit verlaagt, wordt het geheugen opnieuw verplaatst en worden alle elementen in de SortedList<TKey,TValue>.
Deze constructor maakt gebruik van de standaard comparer voor TKey. Als u een vergelijking wilt opgeven, gebruikt u de SortedList<TKey,TValue>(Int32, IComparer<TKey>) constructor. De standaard comparer Comparer<T>.Default controleert of het sleuteltype TKey die implementatie implementeert System.IComparable<T> en gebruikt, indien beschikbaar. Zo niet, Comparer<T>.Default controleert u of het sleuteltype TKey wordt geïmplementeerd System.IComparable. Als het sleuteltype TKey geen van beide interfaces implementeert, kunt u een System.Collections.Generic.IComparer<T> implementatie opgeven in een overbelasting van een constructor die een comparer parameter accepteert.
Deze constructor is een O(n)-bewerking, waarbij n .capacity
Zie ook
Van toepassing op
SortedList<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>)
Initialiseert een nieuw exemplaar van de SortedList<TKey,TValue> klasse die elementen bevat die zijn gekopieerd uit de opgegeven IDictionary<TKey,TValue>, heeft voldoende capaciteit om het aantal gekopieerde elementen te kunnen opvangen en gebruikt de opgegeven IComparer<T>.
public:
SortedList(System::Collections::Generic::IDictionary<TKey, TValue> ^ dictionary, System::Collections::Generic::IComparer<TKey> ^ comparer);
public SortedList(System.Collections.Generic.IDictionary<TKey,TValue> dictionary, System.Collections.Generic.IComparer<TKey> comparer);
new System.Collections.Generic.SortedList<'Key, 'Value> : System.Collections.Generic.IDictionary<'Key, 'Value> * System.Collections.Generic.IComparer<'Key> -> System.Collections.Generic.SortedList<'Key, 'Value>
Public Sub New (dictionary As IDictionary(Of TKey, TValue), comparer As IComparer(Of TKey))
Parameters
- dictionary
- IDictionary<TKey,TValue>
De IDictionary<TKey,TValue> elementen waarvan de elementen naar het nieuwe SortedList<TKey,TValue>worden gekopieerd.
- comparer
- IComparer<TKey>
De IComparer<T> implementatie die moet worden gebruikt bij het vergelijken van sleutels.
– of –
null als u de standaardwaarde Comparer<T> wilt gebruiken voor het type sleutel.
Uitzonderingen
dictionary is null.
dictionary bevat een of meer dubbele sleutels.
Voorbeelden
In het volgende codevoorbeeld ziet u hoe SortedList<TKey,TValue> u een niet-hoofdlettergevoelige, gesorteerde kopie van de informatie maakt in een niet-hoofdlettergevoelige Dictionary<TKey,TValue>, door de Dictionary<TKey,TValue> aan de SortedList<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>) constructor door te geven. In dit voorbeeld zijn de hoofdlettergevoelige vergelijkingen voor de huidige cultuur.
using System;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
// Create a new Dictionary of strings, with string keys and
// a case-insensitive equality comparer for the current
// culture.
Dictionary<string, string> openWith =
new Dictionary<string, string>
(StringComparer.CurrentCultureIgnoreCase);
// Add some elements to the dictionary.
openWith.Add("txt", "notepad.exe");
openWith.Add("Bmp", "paint.exe");
openWith.Add("DIB", "paint.exe");
openWith.Add("rtf", "wordpad.exe");
// Create a SortedList of strings with string keys and a
// case-insensitive equality comparer for the current culture,
// and initialize it with the contents of the Dictionary.
SortedList<string, string> copy =
new SortedList<string, string>(openWith,
StringComparer.CurrentCultureIgnoreCase);
// List the sorted contents of the copy.
Console.WriteLine();
foreach( KeyValuePair<string, string> kvp in copy )
{
Console.WriteLine("Key = {0}, Value = {1}", kvp.Key,
kvp.Value);
}
}
}
/* This code example produces the following output:
Key = Bmp, Value = paint.exe
Key = DIB, Value = paint.exe
Key = rtf, Value = wordpad.exe
Key = txt, Value = notepad.exe
*/
Imports System.Collections.Generic
Public Class Example
Public Shared Sub Main()
' Create a new Dictionary of strings, with string keys and
' a case-insensitive equality comparer for the current
' culture.
Dim openWith As New Dictionary(Of String, String)( _
StringComparer.CurrentCultureIgnoreCase)
' Add some elements to the dictionary.
openWith.Add("txt", "notepad.exe")
openWith.Add("Bmp", "paint.exe")
openWith.Add("DIB", "paint.exe")
openWith.Add("rtf", "wordpad.exe")
' Create a SortedList of strings with string keys and a
' case-insensitive equality comparer for the current culture,
' and initialize it with the contents of the Dictionary.
Dim copy As New SortedList(Of String, String)(openWith, _
StringComparer.CurrentCultureIgnoreCase)
' List the sorted contents of the copy.
Console.WriteLine()
For Each kvp As KeyValuePair(Of String, String) In copy
Console.WriteLine("Key = {0}, Value = {1}", _
kvp.Key, kvp.Value)
Next kvp
End Sub
End Class
' This code example produces the following output:
'
'Key = Bmp, Value = paint.exe
'Key = DIB, Value = paint.exe
'Key = rtf, Value = wordpad.exe
'Key = txt, Value = notepad.exe
Opmerkingen
Elke sleutel in een SortedList<TKey,TValue> moet uniek zijn volgens de opgegeven vergelijkingsfunctie. Op dezelfde manier moet elke sleutel in de bron dictionary ook uniek zijn volgens de opgegeven vergelijkingsfunctie.
De capaciteit van het nieuwe SortedList<TKey,TValue> wordt ingesteld op het aantal elementen in dictionary, dus er vindt geen formaat wijzigen plaats terwijl de lijst wordt gevuld.
De sleutels dictionary worden één keer gekopieerd naar de nieuwe SortedList<TKey,TValue> en gesorteerd, waardoor deze constructor een O(n logboek n) bewerking.
Zie ook
Van toepassing op
SortedList<TKey,TValue>(Int32, IComparer<TKey>)
Initialiseert een nieuw exemplaar van de SortedList<TKey,TValue> klasse die leeg is, heeft de opgegeven initiële capaciteit en gebruikt de opgegeven IComparer<T>.
public:
SortedList(int capacity, System::Collections::Generic::IComparer<TKey> ^ comparer);
public SortedList(int capacity, System.Collections.Generic.IComparer<TKey> comparer);
new System.Collections.Generic.SortedList<'Key, 'Value> : int * System.Collections.Generic.IComparer<'Key> -> System.Collections.Generic.SortedList<'Key, 'Value>
Public Sub New (capacity As Integer, comparer As IComparer(Of TKey))
Parameters
- capacity
- Int32
Het eerste aantal elementen dat de SortedList<TKey,TValue> elementen kunnen bevatten.
- comparer
- IComparer<TKey>
De IComparer<T> implementatie die moet worden gebruikt bij het vergelijken van sleutels.
– of –
null als u de standaardwaarde Comparer<T> wilt gebruiken voor het type sleutel.
Uitzonderingen
capacity is kleiner dan nul.
Voorbeelden
In het volgende codevoorbeeld wordt een gesorteerde lijst gemaakt met een initiële capaciteit van 5 en een niet-hoofdlettergevoelige vergelijking voor de huidige cultuur. In het voorbeeld worden vier elementen toegevoegd, sommige met kleine letters en sommige met hoofdletters. Het voorbeeld probeert vervolgens een element toe te voegen met een sleutel die alleen per geval verschilt van een bestaande sleutel, de resulterende uitzondering onderschept en een foutbericht weergeeft. Ten slotte geeft het voorbeeld de elementen weer in hoofdlettergevoelige sorteervolgorde.
using System;
using System.Collections.Generic;
public class Example
{
public static void Main()
{
// Create a new sorted list of strings, with string keys, an
// initial capacity of 5, and a case-insensitive comparer.
SortedList<string, string> openWith =
new SortedList<string, string>(5,
StringComparer.CurrentCultureIgnoreCase);
// Add 4 elements to the list.
openWith.Add("txt", "notepad.exe");
openWith.Add("bmp", "paint.exe");
openWith.Add("DIB", "paint.exe");
openWith.Add("rtf", "wordpad.exe");
// Try to add a fifth element with a key that is the same
// except for case; this would be allowed with the default
// comparer.
try
{
openWith.Add("BMP", "paint.exe");
}
catch (ArgumentException)
{
Console.WriteLine("\nBMP is already in the sorted list.");
}
// List the contents of the sorted list.
Console.WriteLine();
foreach( KeyValuePair<string, string> kvp in openWith )
{
Console.WriteLine("Key = {0}, Value = {1}", kvp.Key,
kvp.Value);
}
}
}
/* This code example produces the following output:
BMP is already in the sorted list.
Key = bmp, Value = paint.exe
Key = DIB, Value = paint.exe
Key = rtf, Value = wordpad.exe
Key = txt, Value = notepad.exe
*/
Imports System.Collections.Generic
Public Class Example
Public Shared Sub Main()
' Create a new sorted list of strings, with string keys, an
' initial capacity of 5, and a case-insensitive comparer.
Dim openWith As New SortedList(Of String, String)(5, _
StringComparer.CurrentCultureIgnoreCase)
' Add 4 elements to the list.
openWith.Add("txt", "notepad.exe")
openWith.Add("bmp", "paint.exe")
openWith.Add("DIB", "paint.exe")
openWith.Add("rtf", "wordpad.exe")
' Try to add a fifth element with a key that is the same
' except for case; this would be allowed with the default
' comparer.
Try
openWith.Add("BMP", "paint.exe")
Catch ex As ArgumentException
Console.WriteLine(vbLf & "BMP is already in the sorted list.")
End Try
' List the contents of the sorted list.
Console.WriteLine()
For Each kvp As KeyValuePair(Of String, String) In openWith
Console.WriteLine("Key = {0}, Value = {1}", _
kvp.Key, kvp.Value)
Next kvp
End Sub
End Class
' This code example produces the following output:
'
'BMP is already in the sorted list.
'
'Key = bmp, Value = paint.exe
'Key = DIB, Value = paint.exe
'Key = rtf, Value = wordpad.exe
'Key = txt, Value = notepad.exe
Opmerkingen
Elke sleutel in een SortedList<TKey,TValue> moet uniek zijn volgens de opgegeven vergelijkingsfunctie.
De capaciteit van een SortedList<TKey,TValue> is het aantal elementen dat kan worden vastgeslagen voordat u het SortedList<TKey,TValue> formaat wijzigt. Als er elementen aan een SortedList<TKey,TValue>worden toegevoegd, wordt de capaciteit automatisch verhoogd zoals vereist door de interne matrix opnieuw te verplaatsen.
Als de grootte van de verzameling kan worden geschat, hoeft u bij het opgeven van de initiële capaciteit niet meer een aantal groottebewerkingen uit te voeren terwijl er elementen aan de SortedList<TKey,TValue>verzameling worden toegevoegd.
De capaciteit kan worden verlaagd door aan te roepen TrimExcess of door de Capacity eigenschap expliciet in te stellen. Als u de capaciteit verlaagt, wordt het geheugen opnieuw verplaatst en worden alle elementen in de SortedList<TKey,TValue>.
Deze constructor is een O(n)-bewerking, waarbij n .capacity