SortedList<TKey,TValue> Konstruktory

Definice

Inicializuje novou instanci SortedList<TKey,TValue> třídy .

Přetížení

SortedList<TKey,TValue>()

Inicializuje novou instanci SortedList<TKey,TValue> třídy, která je prázdná, má výchozí počáteční kapacitu a použije 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žije zadanou IComparer<T>.

SortedList<TKey,TValue>(IDictionary<TKey,TValue>)

Inicializuje novou instanci SortedList<TKey,TValue> třídy, která obsahuje elementy zkopírované ze zadaného IDictionary<TKey,TValue>objektu , má dostatečnou kapacitu pro přizpůsobení počtu zkopírovaných prvků a použije 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žije výchozí IComparer<T>.

SortedList<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>)

Inicializuje novou instanci SortedList<TKey,TValue> třídy, která obsahuje elementy zkopírované ze zadaného IDictionary<TKey,TValue>objektu , má dostatečnou kapacitu pro přizpůsobení počtu zkopírovaných prvků a použije zadanou 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žije zadanou IComparer<T>.

SortedList<TKey,TValue>()

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žije 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ězec 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á chybu 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 SortedList<TKey,TValue> pro třídu.

// Create a new sorted list of strings, with string
// keys.
SortedList<String^, String^>^ openWith =
    gcnew 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.
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 SortedList<TKey,TValue> souboru musí být jedinečný podle výchozího porovnávače.

Tento konstruktor používá výchozí hodnotu pro počáteční kapacitu objektu SortedList<TKey,TValue>. Pokud chcete nastavit počáteční kapacitu SortedList<TKey,TValue>(Int32) , použijte konstruktor. Pokud je možné odhadnout konečnou velikost kolekce, zadáním počáteční kapacity se eliminuje nutnost provádět několik operací změny velikosti při přidávání prvků do objektu SortedList<TKey,TValue>.

Tento konstruktor používá výchozí porovnávací nástroj pro TKey. Chcete-li určit porovnávací nástroj, použijte SortedList<TKey,TValue>(IComparer<TKey>) konstruktor. Výchozí porovnávací nástroj 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 jestli typ TKey klíče implementuje System.IComparable. Pokud typ TKey klíče neimplementuje ani rozhraní, můžete zadat implementaci System.Collections.Generic.IComparer<T> v přetížení konstruktoru comparer , který přijímá 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

Inicializuje novou instanci SortedList<TKey,TValue> třídy, která je prázdná, má výchozí počáteční kapacitu a použije 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žijte výchozí Comparer<T> hodnotu pro typ klíče.

Příklady

Následující příklad kódu vytvoří seřazený seznam s porovnávačem bez rozlišování velkých a malých písmen pro aktuální jazykovou verzi. Příklad přidá čtyři prvky, některé s klávesami s velkými písmeny a jiné 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 případem, zachytí výslednou výjimku a zobrazí chybovou zprávu. Nakonec příklad zobrazí prvky v pořadí řazení bez rozlišování velký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 SortedList<TKey,TValue> souboru musí být jedinečný podle zadaného porovnávače.

Tento konstruktor používá výchozí hodnotu pro počáteční kapacitu objektu SortedList<TKey,TValue>. Pokud chcete nastavit počáteční kapacitu SortedList<TKey,TValue>(Int32, IComparer<TKey>) , použijte konstruktor. Pokud je možné odhadnout konečnou velikost kolekce, zadáním počáteční kapacity se eliminuje nutnost provádět několik 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

Inicializuje novou instanci SortedList<TKey,TValue> třídy, která obsahuje elementy zkopírované ze zadaného IDictionary<TKey,TValue>objektu , má dostatečnou kapacitu pro přizpůsobení počtu zkopírovaných prvků a použije 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>

Čí IDictionary<TKey,TValue> elementy 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 Dictionary<TKey,TValue>, předáním Dictionary<TKey,TValue> do 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 SortedList<TKey,TValue> souboru musí být jedinečný podle výchozího porovnávače; stejně tak každý klíč ve zdroji dictionary musí být jedinečný podle výchozího porovnávače.

Kapacita nového SortedList<TKey,TValue> souboru je nastavená na počet prvků v dictionary, takže během vyplňování seznamu nedojde ke změně velikosti.

Tento konstruktor používá výchozí porovnávací nástroj pro TKey. Chcete-li určit porovnávací nástroj, použijte SortedList<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>) konstruktor. Výchozí porovnávací nástroj 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 jestli typ TKey klíče implementuje System.IComparable. Pokud typ TKey klíče neimplementuje ani rozhraní, můžete zadat implementaci System.Collections.Generic.IComparer<T> v přetížení konstruktoru comparer , který přijímá parametr.

Klíče v dictionary souboru se zkopírují do nového SortedList<TKey,TValue> a jednou se seřadí, což z tohoto konstruktoru vytvoří operaci protokolu nO(n ).

Viz také

Platí pro

SortedList<TKey,TValue>(Int32)

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žije 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é může obsahovat SortedList<TKey,TValue> .

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 SortedList<TKey,TValue> souboru musí být jedinečný podle výchozího porovnávače.

Kapacita je SortedList<TKey,TValue> počet prvků, které SortedList<TKey,TValue> může obsahovat před změnou velikosti. Při přidání prvků do objektu SortedList<TKey,TValue>se kapacita automaticky zvýší podle potřeby opětovným přidělením interního pole.

Pokud je možné odhadnout velikost kolekce, zadáním počáteční kapacity se 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 Capacity nastavením vlastnosti. Snížení kapacity přerozdělí paměť a zkopíruje všechny prvky v objektu SortedList<TKey,TValue>.

Tento konstruktor používá výchozí porovnávací nástroj pro TKey. Chcete-li určit porovnávací nástroj, použijte SortedList<TKey,TValue>(Int32, IComparer<TKey>) konstruktor. Výchozí porovnávací nástroj 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 jestli typ TKey klíče implementuje System.IComparable. Pokud typ TKey klíče neimplementuje ani rozhraní, můžete zadat implementaci System.Collections.Generic.IComparer<T> v přetížení konstruktoru comparer , který přijímá 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

Inicializuje novou instanci SortedList<TKey,TValue> třídy, která obsahuje elementy zkopírované ze zadaného IDictionary<TKey,TValue>objektu , má dostatečnou kapacitu pro přizpůsobení počtu zkopírovaných prvků a použije zadanou 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>

Čí IDictionary<TKey,TValue> elementy 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žijte 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í kopie informací bez rozlišování malých a malých písmen seřazené informace v nerozlišující malá a malá písmena Dictionary<TKey,TValue>předáním Dictionary<TKey,TValue> do konstruktoru SortedList<TKey,TValue>(IDictionary<TKey,TValue>, IComparer<TKey>) . V tomto příkladu jsou porovnávače 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 SortedList<TKey,TValue> souboru musí být jedinečný podle zadaného porovnávače; stejně tak každý klíč ve zdroji dictionary musí být jedinečný podle zadaného porovnávače.

Kapacita nového SortedList<TKey,TValue> souboru je nastavená na počet prvků v dictionary, takže během vyplňování seznamu nedojde ke změně velikosti.

Klíče v dictionary souboru se zkopírují do nového SortedList<TKey,TValue> a jednou se seřadí, což z tohoto konstruktoru vytvoří operaci protokolu nO(n ).

Viz také

Platí pro

SortedList<TKey,TValue>(Int32, IComparer<TKey>)

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žije 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é může obsahovat SortedList<TKey,TValue> .

comparer
IComparer<TKey>

Implementace IComparer<T> , která se má použít při porovnávání klíčů.

-nebo-

null použijte 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ávačem bez rozlišování velkých a malých písmen pro aktuální jazykovou verzi. Příklad přidá čtyři prvky, některé s klávesami s velkými písmeny a jiné 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 případem, zachytí výslednou výjimku a zobrazí chybovou zprávu. Nakonec příklad zobrazí prvky v pořadí řazení bez rozlišování velký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 SortedList<TKey,TValue> souboru musí být jedinečný podle zadaného porovnávače.

Kapacita je SortedList<TKey,TValue> počet prvků, které SortedList<TKey,TValue> může obsahovat před změnou velikosti. Při přidání prvků do objektu SortedList<TKey,TValue>se kapacita automaticky zvýší podle potřeby opětovným přidělením interního pole.

Pokud je možné odhadnout velikost kolekce, zadáním počáteční kapacity se 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 Capacity nastavením vlastnosti. Snížení kapacity přerozdělí paměť a zkopíruje všechny prvky v objektu SortedList<TKey,TValue>.

Tento konstruktor je operace O(n), kde n je capacity.

Viz také

Platí pro