SortedList<TKey,TValue> Konstruktory
Definice
Důležité
Některé informace platí pro předběžně vydaný produkt, který se může zásadně změnit, než ho výrobce nebo autor vydá. Microsoft neposkytuje žádné záruky, výslovné ani předpokládané, týkající se zde uváděných informací.
Inicializuje novou instanci SortedList<TKey,TValue> třídy.
Přetížení
| Name | Description |
|---|---|
| SortedList<TKey,TValue>() |
Inicializuje novou instanci SortedList<TKey,TValue> třídy, která je prázdná, má výchozí počáteční kapacitu a používá výchozí IComparer<T>. |
| SortedList<TKey,TValue>(IComparer<TKey>) |
Inicializuje novou instanci SortedList<TKey,TValue> třídy, která je prázdná, má výchozí počáteční kapacitu a používá zadanou IComparer<T>. |
| SortedList<TKey,TValue>(IDictionary<TKey,TValue>) |
Inicializuje novou instanci SortedList<TKey,TValue> třídy, která obsahuje prvky zkopírované ze zadaného IDictionary<TKey,TValue>, má dostatečnou kapacitu pro přizpůsobení počtu zkopírovaných prvků a používá výchozí IComparer<T>. |
| SortedList<TKey,TValue>(Int32) |
Inicializuje novou instanci SortedList<TKey,TValue> třídy, která je prázdná, má zadanou počáteční kapacitu a používá výchozí IComparer<T>. |
| SortedList<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>) |
Inicializuje novou instanci SortedList<TKey,TValue> třídy, která obsahuje prvky zkopírované ze zadaného IDictionary<TKey,TValue>, má dostatečnou kapacitu pro přizpůsobení počtu zkopírovaných prvků a používá zadané IComparer<T>. |
| SortedList<TKey,TValue>(Int32, IComparer<TKey>) |
Inicializuje novou instanci SortedList<TKey,TValue> třídy, která je prázdná, má zadanou počáteční kapacitu a používá zadanou IComparer<T>. |
SortedList<TKey,TValue>()
- Zdroj:
- SortedList.cs
- Zdroj:
- SortedList.cs
- Zdroj:
- SortedList.cs
- Zdroj:
- SortedList.cs
- Zdroj:
- SortedList.cs
Inicializuje novou instanci SortedList<TKey,TValue> třídy, která je prázdná, má výchozí počáteční kapacitu a používá výchozí IComparer<T>.
public:
SortedList();
public SortedList();
Public Sub New ()
Příklady
Následující příklad kódu vytvoří prázdné SortedList<TKey,TValue> řetězce s řetězcovými klíči a použije metodu Add k přidání některých prvků. Příklad ukazuje, že Add metoda vyvolá ArgumentException při pokusu o přidání duplicitního klíče.
Tento příklad kódu je součástí většího příkladu uvedeného pro třídu SortedList<TKey,TValue>.
// 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."
Poznámky
Každý klíč v rámci musí SortedList<TKey,TValue> být jedinečný podle výchozího porovnávače.
Tento konstruktor používá výchozí hodnotu pro počáteční kapacitu SortedList<TKey,TValue>. K nastavení počáteční kapacity použijte SortedList<TKey,TValue>(Int32) konstruktor. Pokud lze odhadnout konečnou velikost kolekce, určení počáteční kapacity eliminuje nutnost provádět řadu operací změny velikosti při přidávání prvků do objektu SortedList<TKey,TValue>.
Tento konstruktor používá výchozí porovnávač pro TKey. Chcete-li určit porovnávač, použijte SortedList<TKey,TValue>(IComparer<TKey>) konstruktor. Výchozí porovnávač Comparer<T>.Default zkontroluje, jestli typ TKey klíče implementuje System.IComparable<T> a používá tuto implementaci, pokud je k dispozici. Pokud ne, zkontroluje, Comparer<T>.Default zda typ TKey klíče implementuje System.IComparable. Pokud typ TKey klíče neimplementuje ani rozhraní, můžete zadat System.Collections.Generic.IComparer<T> implementaci v přetížení konstruktoru, který přijímá comparer parametr.
Tento konstruktor je operace O(1).
Viz také
Platí pro
SortedList<TKey,TValue>(IComparer<TKey>)
- Zdroj:
- SortedList.cs
- Zdroj:
- SortedList.cs
- Zdroj:
- SortedList.cs
- Zdroj:
- SortedList.cs
- Zdroj:
- SortedList.cs
Inicializuje novou instanci SortedList<TKey,TValue> třídy, která je prázdná, má výchozí počáteční kapacitu a používá zadanou IComparer<T>.
public:
SortedList(System::Collections::Generic::IComparer<TKey> ^ comparer);
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))
Parametry
- comparer
- IComparer<TKey>
Implementace IComparer<T> , která se má použít při porovnávání klíčů.
nebo
null použít výchozí Comparer<T> hodnotu pro typ klíče.
Příklady
Následující příklad kódu vytvoří seřazený seznam bez rozlišování malých a velkých písmen pro aktuální jazykovou verzi. Příklad přidá čtyři prvky, některé s klíči s velkými písmeny a některé s velkými písmeny. Příklad se pak pokusí přidat prvek s klíčem, který se liší od existujícího klíče pouze v případě, zachytí výslednou výjimku a zobrazí chybovou zprávu. Nakonec příklad zobrazí prvky v pořadí řazení bez rozlišování malých a malých písmen.
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
Poznámky
Každý klíč v určitém SortedList<TKey,TValue> klíči musí být jedinečný podle zadaného porovnávače.
Tento konstruktor používá výchozí hodnotu pro počáteční kapacitu SortedList<TKey,TValue>. K nastavení počáteční kapacity použijte SortedList<TKey,TValue>(Int32, IComparer<TKey>) konstruktor. Pokud lze odhadnout konečnou velikost kolekce, určení počáteční kapacity eliminuje nutnost provádět řadu operací změny velikosti při přidávání prvků do objektu SortedList<TKey,TValue>.
Tento konstruktor je operace O(1).
Viz také
Platí pro
SortedList<TKey,TValue>(IDictionary<TKey,TValue>)
- Zdroj:
- SortedList.cs
- Zdroj:
- SortedList.cs
- Zdroj:
- SortedList.cs
- Zdroj:
- SortedList.cs
- Zdroj:
- SortedList.cs
Inicializuje novou instanci SortedList<TKey,TValue> třídy, která obsahuje prvky zkopírované ze zadaného IDictionary<TKey,TValue>, má dostatečnou kapacitu pro přizpůsobení počtu zkopírovaných prvků a používá výchozí 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))
Parametry
- dictionary
- IDictionary<TKey,TValue>
Jejíž IDictionary<TKey,TValue> prvky jsou zkopírovány do nového SortedList<TKey,TValue>.
Výjimky
dictionary je null.
dictionary obsahuje jeden nebo více duplicitních klíčů.
Příklady
Následující příklad kódu ukazuje, jak použít SortedList<TKey,TValue> k vytvoření seřazené kopie informací v a Dictionary<TKey,TValue>, předáním Dictionary<TKey,TValue> konstruktoru SortedList<TKey,TValue>(IDictionary<TKey,TValue>) .
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
Poznámky
Každý klíč v objektu SortedList<TKey,TValue> musí být jedinečný podle výchozího porovnávače. Stejně tak musí být každý klíč ve zdroji dictionary jedinečný také podle výchozího porovnávače.
Kapacita nového SortedList<TKey,TValue> je nastavena na počet prvků v dictionary, takže se při vyplňování seznamu neprovádí žádná změna velikosti.
Tento konstruktor používá výchozí porovnávač pro TKey. Chcete-li určit porovnávač, použijte SortedList<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>) konstruktor. Výchozí porovnávač Comparer<T>.Default zkontroluje, jestli typ TKey klíče implementuje System.IComparable<T> a používá tuto implementaci, pokud je k dispozici. Pokud ne, zkontroluje, Comparer<T>.Default zda typ TKey klíče implementuje System.IComparable. Pokud typ TKey klíče neimplementuje ani rozhraní, můžete zadat System.Collections.Generic.IComparer<T> implementaci v přetížení konstruktoru, který přijímá comparer parametr.
Klíče se dictionary zkopírují do nového SortedList<TKey,TValue> a seřadí jednou, což z tohoto konstruktoru dělá operaci O(n log n).
Viz také
Platí pro
SortedList<TKey,TValue>(Int32)
- Zdroj:
- SortedList.cs
- Zdroj:
- SortedList.cs
- Zdroj:
- SortedList.cs
- Zdroj:
- SortedList.cs
- Zdroj:
- SortedList.cs
Inicializuje novou instanci SortedList<TKey,TValue> třídy, která je prázdná, má zadanou počáteční kapacitu a používá výchozí 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)
Parametry
- capacity
- Int32
Počáteční počet prvků, které SortedList<TKey,TValue> může obsahovat.
Výjimky
Hodnota capacity je menší než nula.
Příklady
Následující příklad kódu vytvoří seřazený seznam s počáteční kapacitou 4 a naplní ho 4 položkami.
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
Poznámky
Každý klíč v rámci musí SortedList<TKey,TValue> být jedinečný podle výchozího porovnávače.
Kapacita prvku SortedList<TKey,TValue> je počet prvků, které SortedList<TKey,TValue> může před změnou velikosti obsahovat. Vzhledem k tomu, že jsou prvky přidány do objektu SortedList<TKey,TValue>, kapacita se automaticky zvýší podle potřeby přidělením interního pole.
Pokud lze odhadnout velikost kolekce, určení počáteční kapacity eliminuje nutnost provádět několik operací změny velikosti při přidávání prvků do objektu SortedList<TKey,TValue>.
Kapacitu lze snížit voláním TrimExcess nebo explicitním nastavením Capacity vlastnosti. Snížení kapacity relokuje paměť a zkopíruje všechny prvky v objektu SortedList<TKey,TValue>.
Tento konstruktor používá výchozí porovnávač pro TKey. Chcete-li určit porovnávač, použijte SortedList<TKey,TValue>(Int32, IComparer<TKey>) konstruktor. Výchozí porovnávač Comparer<T>.Default zkontroluje, jestli typ TKey klíče implementuje System.IComparable<T> a používá tuto implementaci, pokud je k dispozici. Pokud ne, zkontroluje, Comparer<T>.Default zda typ TKey klíče implementuje System.IComparable. Pokud typ TKey klíče neimplementuje ani rozhraní, můžete zadat System.Collections.Generic.IComparer<T> implementaci v přetížení konstruktoru, který přijímá comparer parametr.
Tento konstruktor je operace O(n), kde n je capacity.
Viz také
Platí pro
SortedList<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>)
- Zdroj:
- SortedList.cs
- Zdroj:
- SortedList.cs
- Zdroj:
- SortedList.cs
- Zdroj:
- SortedList.cs
- Zdroj:
- SortedList.cs
Inicializuje novou instanci SortedList<TKey,TValue> třídy, která obsahuje prvky zkopírované ze zadaného IDictionary<TKey,TValue>, má dostatečnou kapacitu pro přizpůsobení počtu zkopírovaných prvků a používá zadané 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);
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))
Parametry
- dictionary
- IDictionary<TKey,TValue>
Jejíž IDictionary<TKey,TValue> prvky jsou zkopírovány do nového SortedList<TKey,TValue>.
- comparer
- IComparer<TKey>
Implementace IComparer<T> , která se má použít při porovnávání klíčů.
nebo
null použít výchozí Comparer<T> hodnotu pro typ klíče.
Výjimky
dictionary je null.
dictionary obsahuje jeden nebo více duplicitních klíčů.
Příklady
Následující příklad kódu ukazuje, jak použít SortedList<TKey,TValue> k vytvoření seřazené kopie informací Dictionary<TKey,TValue>nerozlišující velká a malá písmena , předáním Dictionary<TKey,TValue> konstruktoru SortedList<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>) . V tomto příkladu jsou porovnání nerozlišující malá a velká písmena pro aktuální jazykovou verzi.
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
Poznámky
Každý klíč v objektu SortedList<TKey,TValue> musí být jedinečný podle zadaného porovnávače. Stejně tak musí být každý klíč ve zdroji dictionary jedinečný také podle zadaného porovnávače.
Kapacita nového SortedList<TKey,TValue> je nastavena na počet prvků v dictionary, takže se při vyplňování seznamu neprovádí žádná změna velikosti.
Klíče se dictionary zkopírují do nového SortedList<TKey,TValue> a seřadí jednou, což z tohoto konstruktoru dělá operaci O(n log n).
Viz také
Platí pro
SortedList<TKey,TValue>(Int32, IComparer<TKey>)
- Zdroj:
- SortedList.cs
- Zdroj:
- SortedList.cs
- Zdroj:
- SortedList.cs
- Zdroj:
- SortedList.cs
- Zdroj:
- SortedList.cs
Inicializuje novou instanci SortedList<TKey,TValue> třídy, která je prázdná, má zadanou počáteční kapacitu a používá zadanou IComparer<T>.
public:
SortedList(int capacity, System::Collections::Generic::IComparer<TKey> ^ comparer);
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))
Parametry
- capacity
- Int32
Počáteční počet prvků, které SortedList<TKey,TValue> může obsahovat.
- comparer
- IComparer<TKey>
Implementace IComparer<T> , která se má použít při porovnávání klíčů.
nebo
null použít výchozí Comparer<T> hodnotu pro typ klíče.
Výjimky
Hodnota capacity je menší než nula.
Příklady
Následující příklad kódu vytvoří seřazený seznam s počáteční kapacitou 5 a porovnání nerozlišující malá a velká písmena pro aktuální jazykovou verzi. Příklad přidá čtyři prvky, některé s klíči s velkými písmeny a některé s velkými písmeny. Příklad se pak pokusí přidat prvek s klíčem, který se liší od existujícího klíče pouze v případě, zachytí výslednou výjimku a zobrazí chybovou zprávu. Nakonec příklad zobrazí prvky v pořadí řazení bez rozlišování malých a malých písmen.
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
Poznámky
Každý klíč v určitém SortedList<TKey,TValue> klíči musí být jedinečný podle zadaného porovnávače.
Kapacita prvku SortedList<TKey,TValue> je počet prvků, které SortedList<TKey,TValue> může před změnou velikosti obsahovat. Vzhledem k tomu, že jsou prvky přidány do objektu SortedList<TKey,TValue>, kapacita se automaticky zvýší podle potřeby přidělením interního pole.
Pokud lze odhadnout velikost kolekce, určení počáteční kapacity eliminuje nutnost provádět několik operací změny velikosti při přidávání prvků do objektu SortedList<TKey,TValue>.
Kapacitu lze snížit voláním TrimExcess nebo explicitním nastavením Capacity vlastnosti. Snížení kapacity relokuje paměť a zkopíruje všechny prvky v objektu SortedList<TKey,TValue>.
Tento konstruktor je operace O(n), kde n je capacity.