SortedList Oluşturucular
Tanım
Önemli
Bazı bilgiler ürünün ön sürümüyle ilgilidir ve sürüm öncesinde önemli değişiklikler yapılmış olabilir. Burada verilen bilgilerle ilgili olarak Microsoft açık veya zımni hiçbir garanti vermez.
SortedList sınıfının yeni bir örneğini başlatır.
Aşırı Yüklemeler
SortedList() |
Boş olan, varsayılan başlangıç kapasitesine sahip olan ve nesneye eklenen her anahtar tarafından uygulanan arabirime IComparable göre sıralanan sınıfın yeni bir örneğini SortedListSortedList başlatır. |
SortedList(IComparer) |
Sınıfın SortedList boş, varsayılan başlangıç kapasitesine sahip ve belirtilen IComparer arabirime göre sıralanmış yeni bir örneğini başlatır. |
SortedList(IDictionary) |
Belirtilen sözlükten kopyalanan öğeleri içeren sınıfının yeni bir örneğini SortedList başlatır, kopyalanan öğelerin sayısıyla aynı başlangıç kapasitesine sahiptir ve her anahtar tarafından uygulanan arabirime IComparable göre sıralanır. |
SortedList(Int32) |
Boş olan, belirtilen başlangıç kapasitesine sahip olan ve nesneye eklenen her anahtar tarafından uygulanan arabirime IComparable göre sıralanan yeni bir sınıf örneğini SortedListSortedList başlatır. |
SortedList(IComparer, Int32) |
Sınıfın SortedList boş, belirtilen başlangıç kapasitesine sahip ve belirtilen IComparer arabirime göre sıralanmış yeni bir örneğini başlatır. |
SortedList(IDictionary, IComparer) |
Belirtilen sözlükten kopyalanan öğeleri içeren sınıfının yeni bir örneğini SortedList başlatır, kopyalanan öğe sayısıyla aynı başlangıç kapasitesine sahiptir ve belirtilen IComparer arabirime göre sıralanır. |
SortedList()
- Kaynak:
- SortedList.cs
- Kaynak:
- SortedList.cs
- Kaynak:
- SortedList.cs
Boş olan, varsayılan başlangıç kapasitesine sahip olan ve nesneye eklenen her anahtar tarafından uygulanan arabirime IComparable göre sıralanan sınıfın yeni bir örneğini SortedListSortedList başlatır.
public:
SortedList();
public SortedList ();
Public Sub New ()
Örnekler
Aşağıdaki kod örneği, farklı SortedList oluşturucuları kullanarak koleksiyonlar oluşturur ve koleksiyonların davranışındaki farkları gösterir.
// The following code example creates SortedList instances using different constructors
// and demonstrates the differences in the behavior of the SortedList instances.
using namespace System;
using namespace System::Collections;
using namespace System::Globalization;
void PrintKeysAndValues( SortedList^ myList )
{
Console::WriteLine( " -KEY- -VALUE-" );
for ( int i = 0; i < myList->Count; i++ )
{
Console::WriteLine( " {0,-6}: {1}", myList->GetKey( i ), myList->GetByIndex( i ) );
}
Console::WriteLine();
}
int main()
{
// Create a SortedList using the default comparer.
SortedList^ mySL1 = gcnew SortedList;
Console::WriteLine( "mySL1 (default):" );
mySL1->Add( "FIRST", "Hello" );
mySL1->Add( "SECOND", "World" );
mySL1->Add( "THIRD", "!" );
try { mySL1->Add( "first", "Ola!" ); }
catch ( ArgumentException^ e ) { Console::WriteLine( e ); }
PrintKeysAndValues( mySL1 );
// Create a SortedList using the specified case-insensitive comparer.
SortedList^ mySL2 = gcnew SortedList( gcnew CaseInsensitiveComparer );
Console::WriteLine( "mySL2 (case-insensitive comparer):" );
mySL2->Add( "FIRST", "Hello" );
mySL2->Add( "SECOND", "World" );
mySL2->Add( "THIRD", "!" );
try { mySL2->Add( "first", "Ola!" ); }
catch ( ArgumentException^ e ) { Console::WriteLine( e ); }
PrintKeysAndValues( mySL2 );
// Create a SortedList using the specified KeyComparer.
// The KeyComparer uses a case-insensitive hash code provider and a case-insensitive comparer,
// which are based on the Turkish culture (tr-TR), where "I" is not the uppercase version of "i".
CultureInfo^ myCul = gcnew CultureInfo( "tr-TR" );
SortedList^ mySL3 = gcnew SortedList( gcnew CaseInsensitiveComparer( myCul ) );
Console::WriteLine( "mySL3 (case-insensitive comparer, Turkish culture):" );
mySL3->Add( "FIRST", "Hello" );
mySL3->Add( "SECOND", "World" );
mySL3->Add( "THIRD", "!" );
try { mySL3->Add( "first", "Ola!" ); }
catch ( ArgumentException^ e ) { Console::WriteLine( e ); }
PrintKeysAndValues( mySL3 );
// Create a SortedList using the ComparisonType.InvariantCultureIgnoreCase value.
SortedList^ mySL4 = gcnew SortedList( StringComparer::InvariantCultureIgnoreCase );
Console::WriteLine( "mySL4 (InvariantCultureIgnoreCase):" );
mySL4->Add( "FIRST", "Hello" );
mySL4->Add( "SECOND", "World" );
mySL4->Add( "THIRD", "!" );
try { mySL4->Add( "first", "Ola!" ); }
catch ( ArgumentException^ e ) { Console::WriteLine( e ); }
PrintKeysAndValues( mySL4 );
Console::WriteLine("\n\nHit ENTER to return");
Console::ReadLine();
}
/*
This code produces the following output. Results vary depending on the system's culture settings.
mySL1 (default):
-KEY- -VALUE-
first : Ola!
FIRST : Hello
SECOND: World
THIRD : !
mySL2 (case-insensitive comparer):
System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'
at System.Collections.SortedList.Add(Object key, Object value)
at SamplesSortedList.Main()
-KEY- -VALUE-
FIRST : Hello
SECOND: World
THIRD : !
mySL3 (case-insensitive comparer, Turkish culture):
-KEY- -VALUE-
FIRST : Hello
first : Ola!
SECOND: World
THIRD : !
mySL4 (InvariantCultureIgnoreCase):
System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'
at System.Collections.SortedList.Add(Object key, Object value)
at SamplesSortedList.Main()
-KEY- -VALUE-
FIRST : Hello
SECOND: World
THIRD : !
*/
using System;
using System.Collections;
using System.Globalization;
public class SamplesSortedList
{
public static void Main()
{
// Create a SortedList using the default comparer.
SortedList mySL1 = new SortedList();
Console.WriteLine("mySL1 (default):");
mySL1.Add("FIRST", "Hello");
mySL1.Add("SECOND", "World");
mySL1.Add("THIRD", "!");
try
{
mySL1.Add("first", "Ola!");
}
catch (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL1);
// Create a SortedList using the specified case-insensitive comparer.
SortedList mySL2 = new SortedList(new CaseInsensitiveComparer());
Console.WriteLine("mySL2 (case-insensitive comparer):");
mySL2.Add("FIRST", "Hello");
mySL2.Add("SECOND", "World");
mySL2.Add("THIRD", "!");
try
{
mySL2.Add("first", "Ola!");
}
catch (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL2);
// Create a SortedList using the specified CaseInsensitiveComparer,
// which is based on the Turkish culture (tr-TR), where "I" is not
// the uppercase version of "i".
CultureInfo myCul = new CultureInfo("tr-TR");
SortedList mySL3 = new SortedList(new CaseInsensitiveComparer(myCul));
Console.WriteLine(
"mySL3 (case-insensitive comparer, Turkish culture):");
mySL3.Add("FIRST", "Hello");
mySL3.Add("SECOND", "World");
mySL3.Add("THIRD", "!");
try
{
mySL3.Add("first", "Ola!");
}
catch (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL3);
// Create a SortedList using the
// StringComparer.InvariantCultureIgnoreCase value.
SortedList mySL4 = new SortedList(
StringComparer.InvariantCultureIgnoreCase);
Console.WriteLine("mySL4 (InvariantCultureIgnoreCase):");
mySL4.Add("FIRST", "Hello");
mySL4.Add("SECOND", "World");
mySL4.Add("THIRD", "!");
try
{
mySL4.Add("first", "Ola!");
}
catch (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL4);
}
public static void PrintKeysAndValues(SortedList myList)
{
Console.WriteLine(" -KEY- -VALUE-");
for (int i = 0; i < myList.Count; i++)
{
Console.WriteLine(" {0,-6}: {1}",
myList.GetKey(i), myList.GetByIndex(i));
}
Console.WriteLine();
}
}
/*
This code produces the following output.
Results vary depending on the system's culture settings.
mySL1 (default):
-KEY- -VALUE-
first : Ola!
FIRST : Hello
SECOND: World
THIRD : !
mySL2 (case-insensitive comparer):
System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'
at System.Collections.SortedList.Add(Object key, Object value)
at SamplesSortedList.Main()
-KEY- -VALUE-
FIRST : Hello
SECOND: World
THIRD : !
mySL3 (case-insensitive comparer, Turkish culture):
-KEY- -VALUE-
FIRST : Hello
first : Ola!
SECOND: World
THIRD : !
mySL4 (InvariantCultureIgnoreCase):
System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'
at System.Collections.SortedList.Add(Object key, Object value)
at SamplesSortedList.Main()
-KEY- -VALUE-
FIRST : Hello
SECOND: World
THIRD : !
*/
Imports System.Collections
Imports System.Globalization
Public Class SamplesSortedList
Public Shared Sub Main()
' Create a SortedList using the default comparer.
Dim mySL1 As New SortedList()
Console.WriteLine("mySL1 (default):")
mySL1.Add("FIRST", "Hello")
mySL1.Add("SECOND", "World")
mySL1.Add("THIRD", "!")
Try
mySL1.Add("first", "Ola!")
Catch e As ArgumentException
Console.WriteLine(e)
End Try
PrintKeysAndValues(mySL1)
' Create a SortedList using the specified case-insensitive comparer.
Dim mySL2 As New SortedList(New CaseInsensitiveComparer())
Console.WriteLine("mySL2 (case-insensitive comparer):")
mySL2.Add("FIRST", "Hello")
mySL2.Add("SECOND", "World")
mySL2.Add("THIRD", "!")
Try
mySL2.Add("first", "Ola!")
Catch e As ArgumentException
Console.WriteLine(e)
End Try
PrintKeysAndValues(mySL2)
' Create a SortedList using the specified CaseInsensitiveComparer,
' which is based on the Turkish culture (tr-TR), where "I" is not
' the uppercase version of "i".
Dim myCul As New CultureInfo("tr-TR")
Dim mySL3 As New SortedList(New CaseInsensitiveComparer(myCul))
Console.WriteLine("mySL3 (case-insensitive comparer, Turkish culture):")
mySL3.Add("FIRST", "Hello")
mySL3.Add("SECOND", "World")
mySL3.Add("THIRD", "!")
Try
mySL3.Add("first", "Ola!")
Catch e As ArgumentException
Console.WriteLine(e)
End Try
PrintKeysAndValues(mySL3)
' Create a SortedList using the
' StringComparer.InvariantCultureIgnoreCase value.
Dim mySL4 As New SortedList( _
StringComparer.InvariantCultureIgnoreCase)
Console.WriteLine("mySL4 (InvariantCultureIgnoreCase):")
mySL4.Add("FIRST", "Hello")
mySL4.Add("SECOND", "World")
mySL4.Add("THIRD", "!")
Try
mySL4.Add("first", "Ola!")
Catch e As ArgumentException
Console.WriteLine(e)
End Try
PrintKeysAndValues(mySL4)
End Sub
Public Shared Sub PrintKeysAndValues(ByVal myList As SortedList)
Console.WriteLine(" -KEY- -VALUE-")
Dim i As Integer
For i = 0 To myList.Count - 1
Console.WriteLine(" {0,-6}: {1}", _
myList.GetKey(i), myList.GetByIndex(i))
Next i
Console.WriteLine()
End Sub
End Class
'This code produces the following output. Results vary depending on the system's culture settings.
'
'mySL1 (default):
' -KEY- -VALUE-
' first : Ola!
' FIRST : Hello
' SECOND: World
' THIRD : !
'
'mySL2 (case-insensitive comparer):
'System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'' at System.Collections.SortedList.Add(Object key, Object value)
' at SamplesSortedList.Main()
' -KEY- -VALUE-
' FIRST : Hello
' SECOND: World
' THIRD : !
'
'mySL3 (case-insensitive comparer, Turkish culture):
' -KEY- -VALUE-
' FIRST : Hello
' first : Ola!
' SECOND: World
' THIRD : !
'
'mySL4 (InvariantCultureIgnoreCase):
'System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'' at System.Collections.SortedList.Add(Object key, Object value)
' at SamplesSortedList.Main()
' -KEY- -VALUE-
' FIRST : Hello
' SECOND: World
' THIRD : !
Açıklamalar
Her anahtar, nesnedeki IComparable diğer tüm anahtarlarla karşılaştırma yapabilmesi için arabirimini SortedList uygulamalıdır. Öğeler, öğesine eklenen her anahtarın uygulamasına SortedListgöre IComparable sıralanır.
Bir SortedList nesnenin kapasitesi, öğesinin barındırabileceği öğe SortedList sayısıdır. öğesine SortedListöğe eklendikçe, iç dizi yeniden konumlandırılarak kapasite gerektiği gibi otomatik olarak artırılır.
Koleksiyonun boyutu tahmin edilebilirse, ilk kapasitenin belirtilmesi, nesneye öğe eklerken bir dizi yeniden boyutlandırma işlemi gerçekleştirme gereksinimini SortedList ortadan kaldırır.
Bu oluşturucu bir O(1)
işlemdir.
Ayrıca bkz.
Şunlara uygulanır
SortedList(IComparer)
- Kaynak:
- SortedList.cs
- Kaynak:
- SortedList.cs
- Kaynak:
- SortedList.cs
Sınıfın SortedList boş, varsayılan başlangıç kapasitesine sahip ve belirtilen IComparer arabirime göre sıralanmış yeni bir örneğini başlatır.
public:
SortedList(System::Collections::IComparer ^ comparer);
public SortedList (System.Collections.IComparer comparer);
public SortedList (System.Collections.IComparer? comparer);
new System.Collections.SortedList : System.Collections.IComparer -> System.Collections.SortedList
Public Sub New (comparer As IComparer)
Parametreler
- comparer
- IComparer
IComparer Anahtarları karşılaştırırken kullanılacak uygulama.
-veya-
null
her anahtarın IComparable uygulamasını kullanmak için.
Örnekler
Aşağıdaki kod örneği, farklı SortedList oluşturucuları kullanarak koleksiyonlar oluşturur ve koleksiyonların davranışındaki farkları gösterir.
// The following code example creates SortedList instances using different constructors
// and demonstrates the differences in the behavior of the SortedList instances.
using namespace System;
using namespace System::Collections;
using namespace System::Globalization;
void PrintKeysAndValues( SortedList^ myList )
{
Console::WriteLine( " -KEY- -VALUE-" );
for ( int i = 0; i < myList->Count; i++ )
{
Console::WriteLine( " {0,-6}: {1}", myList->GetKey( i ), myList->GetByIndex( i ) );
}
Console::WriteLine();
}
int main()
{
// Create a SortedList using the default comparer.
SortedList^ mySL1 = gcnew SortedList;
Console::WriteLine( "mySL1 (default):" );
mySL1->Add( "FIRST", "Hello" );
mySL1->Add( "SECOND", "World" );
mySL1->Add( "THIRD", "!" );
try { mySL1->Add( "first", "Ola!" ); }
catch ( ArgumentException^ e ) { Console::WriteLine( e ); }
PrintKeysAndValues( mySL1 );
// Create a SortedList using the specified case-insensitive comparer.
SortedList^ mySL2 = gcnew SortedList( gcnew CaseInsensitiveComparer );
Console::WriteLine( "mySL2 (case-insensitive comparer):" );
mySL2->Add( "FIRST", "Hello" );
mySL2->Add( "SECOND", "World" );
mySL2->Add( "THIRD", "!" );
try { mySL2->Add( "first", "Ola!" ); }
catch ( ArgumentException^ e ) { Console::WriteLine( e ); }
PrintKeysAndValues( mySL2 );
// Create a SortedList using the specified KeyComparer.
// The KeyComparer uses a case-insensitive hash code provider and a case-insensitive comparer,
// which are based on the Turkish culture (tr-TR), where "I" is not the uppercase version of "i".
CultureInfo^ myCul = gcnew CultureInfo( "tr-TR" );
SortedList^ mySL3 = gcnew SortedList( gcnew CaseInsensitiveComparer( myCul ) );
Console::WriteLine( "mySL3 (case-insensitive comparer, Turkish culture):" );
mySL3->Add( "FIRST", "Hello" );
mySL3->Add( "SECOND", "World" );
mySL3->Add( "THIRD", "!" );
try { mySL3->Add( "first", "Ola!" ); }
catch ( ArgumentException^ e ) { Console::WriteLine( e ); }
PrintKeysAndValues( mySL3 );
// Create a SortedList using the ComparisonType.InvariantCultureIgnoreCase value.
SortedList^ mySL4 = gcnew SortedList( StringComparer::InvariantCultureIgnoreCase );
Console::WriteLine( "mySL4 (InvariantCultureIgnoreCase):" );
mySL4->Add( "FIRST", "Hello" );
mySL4->Add( "SECOND", "World" );
mySL4->Add( "THIRD", "!" );
try { mySL4->Add( "first", "Ola!" ); }
catch ( ArgumentException^ e ) { Console::WriteLine( e ); }
PrintKeysAndValues( mySL4 );
Console::WriteLine("\n\nHit ENTER to return");
Console::ReadLine();
}
/*
This code produces the following output. Results vary depending on the system's culture settings.
mySL1 (default):
-KEY- -VALUE-
first : Ola!
FIRST : Hello
SECOND: World
THIRD : !
mySL2 (case-insensitive comparer):
System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'
at System.Collections.SortedList.Add(Object key, Object value)
at SamplesSortedList.Main()
-KEY- -VALUE-
FIRST : Hello
SECOND: World
THIRD : !
mySL3 (case-insensitive comparer, Turkish culture):
-KEY- -VALUE-
FIRST : Hello
first : Ola!
SECOND: World
THIRD : !
mySL4 (InvariantCultureIgnoreCase):
System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'
at System.Collections.SortedList.Add(Object key, Object value)
at SamplesSortedList.Main()
-KEY- -VALUE-
FIRST : Hello
SECOND: World
THIRD : !
*/
using System;
using System.Collections;
using System.Globalization;
public class SamplesSortedList
{
public static void Main()
{
// Create a SortedList using the default comparer.
SortedList mySL1 = new SortedList();
Console.WriteLine("mySL1 (default):");
mySL1.Add("FIRST", "Hello");
mySL1.Add("SECOND", "World");
mySL1.Add("THIRD", "!");
try
{
mySL1.Add("first", "Ola!");
}
catch (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL1);
// Create a SortedList using the specified case-insensitive comparer.
SortedList mySL2 = new SortedList(new CaseInsensitiveComparer());
Console.WriteLine("mySL2 (case-insensitive comparer):");
mySL2.Add("FIRST", "Hello");
mySL2.Add("SECOND", "World");
mySL2.Add("THIRD", "!");
try
{
mySL2.Add("first", "Ola!");
}
catch (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL2);
// Create a SortedList using the specified CaseInsensitiveComparer,
// which is based on the Turkish culture (tr-TR), where "I" is not
// the uppercase version of "i".
CultureInfo myCul = new CultureInfo("tr-TR");
SortedList mySL3 = new SortedList(new CaseInsensitiveComparer(myCul));
Console.WriteLine(
"mySL3 (case-insensitive comparer, Turkish culture):");
mySL3.Add("FIRST", "Hello");
mySL3.Add("SECOND", "World");
mySL3.Add("THIRD", "!");
try
{
mySL3.Add("first", "Ola!");
}
catch (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL3);
// Create a SortedList using the
// StringComparer.InvariantCultureIgnoreCase value.
SortedList mySL4 = new SortedList(
StringComparer.InvariantCultureIgnoreCase);
Console.WriteLine("mySL4 (InvariantCultureIgnoreCase):");
mySL4.Add("FIRST", "Hello");
mySL4.Add("SECOND", "World");
mySL4.Add("THIRD", "!");
try
{
mySL4.Add("first", "Ola!");
}
catch (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL4);
}
public static void PrintKeysAndValues(SortedList myList)
{
Console.WriteLine(" -KEY- -VALUE-");
for (int i = 0; i < myList.Count; i++)
{
Console.WriteLine(" {0,-6}: {1}",
myList.GetKey(i), myList.GetByIndex(i));
}
Console.WriteLine();
}
}
/*
This code produces the following output.
Results vary depending on the system's culture settings.
mySL1 (default):
-KEY- -VALUE-
first : Ola!
FIRST : Hello
SECOND: World
THIRD : !
mySL2 (case-insensitive comparer):
System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'
at System.Collections.SortedList.Add(Object key, Object value)
at SamplesSortedList.Main()
-KEY- -VALUE-
FIRST : Hello
SECOND: World
THIRD : !
mySL3 (case-insensitive comparer, Turkish culture):
-KEY- -VALUE-
FIRST : Hello
first : Ola!
SECOND: World
THIRD : !
mySL4 (InvariantCultureIgnoreCase):
System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'
at System.Collections.SortedList.Add(Object key, Object value)
at SamplesSortedList.Main()
-KEY- -VALUE-
FIRST : Hello
SECOND: World
THIRD : !
*/
Imports System.Collections
Imports System.Globalization
Public Class SamplesSortedList
Public Shared Sub Main()
' Create a SortedList using the default comparer.
Dim mySL1 As New SortedList()
Console.WriteLine("mySL1 (default):")
mySL1.Add("FIRST", "Hello")
mySL1.Add("SECOND", "World")
mySL1.Add("THIRD", "!")
Try
mySL1.Add("first", "Ola!")
Catch e As ArgumentException
Console.WriteLine(e)
End Try
PrintKeysAndValues(mySL1)
' Create a SortedList using the specified case-insensitive comparer.
Dim mySL2 As New SortedList(New CaseInsensitiveComparer())
Console.WriteLine("mySL2 (case-insensitive comparer):")
mySL2.Add("FIRST", "Hello")
mySL2.Add("SECOND", "World")
mySL2.Add("THIRD", "!")
Try
mySL2.Add("first", "Ola!")
Catch e As ArgumentException
Console.WriteLine(e)
End Try
PrintKeysAndValues(mySL2)
' Create a SortedList using the specified CaseInsensitiveComparer,
' which is based on the Turkish culture (tr-TR), where "I" is not
' the uppercase version of "i".
Dim myCul As New CultureInfo("tr-TR")
Dim mySL3 As New SortedList(New CaseInsensitiveComparer(myCul))
Console.WriteLine("mySL3 (case-insensitive comparer, Turkish culture):")
mySL3.Add("FIRST", "Hello")
mySL3.Add("SECOND", "World")
mySL3.Add("THIRD", "!")
Try
mySL3.Add("first", "Ola!")
Catch e As ArgumentException
Console.WriteLine(e)
End Try
PrintKeysAndValues(mySL3)
' Create a SortedList using the
' StringComparer.InvariantCultureIgnoreCase value.
Dim mySL4 As New SortedList( _
StringComparer.InvariantCultureIgnoreCase)
Console.WriteLine("mySL4 (InvariantCultureIgnoreCase):")
mySL4.Add("FIRST", "Hello")
mySL4.Add("SECOND", "World")
mySL4.Add("THIRD", "!")
Try
mySL4.Add("first", "Ola!")
Catch e As ArgumentException
Console.WriteLine(e)
End Try
PrintKeysAndValues(mySL4)
End Sub
Public Shared Sub PrintKeysAndValues(ByVal myList As SortedList)
Console.WriteLine(" -KEY- -VALUE-")
Dim i As Integer
For i = 0 To myList.Count - 1
Console.WriteLine(" {0,-6}: {1}", _
myList.GetKey(i), myList.GetByIndex(i))
Next i
Console.WriteLine()
End Sub
End Class
'This code produces the following output. Results vary depending on the system's culture settings.
'
'mySL1 (default):
' -KEY- -VALUE-
' first : Ola!
' FIRST : Hello
' SECOND: World
' THIRD : !
'
'mySL2 (case-insensitive comparer):
'System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'' at System.Collections.SortedList.Add(Object key, Object value)
' at SamplesSortedList.Main()
' -KEY- -VALUE-
' FIRST : Hello
' SECOND: World
' THIRD : !
'
'mySL3 (case-insensitive comparer, Turkish culture):
' -KEY- -VALUE-
' FIRST : Hello
' first : Ola!
' SECOND: World
' THIRD : !
'
'mySL4 (InvariantCultureIgnoreCase):
'System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'' at System.Collections.SortedList.Add(Object key, Object value)
' at SamplesSortedList.Main()
' -KEY- -VALUE-
' FIRST : Hello
' SECOND: World
' THIRD : !
Açıklamalar
Öğeler belirtilen IComparer uygulamaya göre sıralanır. comparer
parametresi isenull
IComparable, her anahtarın uygulanması kullanılır; bu nedenle, her anahtarın nesnedeki IComparableSortedList diğer tüm anahtarlarla karşılaştırma yapabilmesi için arabirimi uygulaması gerekir.
Bir SortedList nesnenin kapasitesi, öğesinin barındırabileceği öğe SortedList sayısıdır. öğesine SortedListöğe eklendikçe, iç dizi yeniden konumlandırılarak kapasite gerektiği gibi otomatik olarak artırılır.
Koleksiyonun boyutu tahmin edilebilirse, ilk kapasitenin belirtilmesi, nesneye öğe eklerken bir dizi yeniden boyutlandırma işlemi gerçekleştirme gereksinimini SortedList ortadan kaldırır.
Bu oluşturucu bir O(1)
işlemdir.
Ayrıca bkz.
Şunlara uygulanır
SortedList(IDictionary)
- Kaynak:
- SortedList.cs
- Kaynak:
- SortedList.cs
- Kaynak:
- SortedList.cs
Belirtilen sözlükten kopyalanan öğeleri içeren sınıfının yeni bir örneğini SortedList başlatır, kopyalanan öğelerin sayısıyla aynı başlangıç kapasitesine sahiptir ve her anahtar tarafından uygulanan arabirime IComparable göre sıralanır.
public:
SortedList(System::Collections::IDictionary ^ d);
public SortedList (System.Collections.IDictionary d);
new System.Collections.SortedList : System.Collections.IDictionary -> System.Collections.SortedList
Public Sub New (d As IDictionary)
Parametreler
IDictionary Yeni SortedList bir nesneye kopyalanacak uygulama.
Özel durumlar
d
, null
değeridir.
içindeki d
bir veya daha fazla öğe arabirimini IComparable uygulamaz.
Örnekler
Aşağıdaki kod örneği, farklı SortedList oluşturucuları kullanarak koleksiyonlar oluşturur ve koleksiyonların davranışındaki farkları gösterir.
using namespace System;
using namespace System::Collections;
using namespace System::Globalization;
void PrintKeysAndValues( SortedList^ myList )
{
Console::WriteLine( " -KEY- -VALUE-" );
for ( int i = 0; i < myList->Count; i++ )
{
Console::WriteLine( " {0,-6}: {1}", myList->GetKey( i ), myList->GetByIndex( i ) );
}
Console::WriteLine();
}
int main()
{
// Create the dictionary.
Hashtable^ myHT = gcnew Hashtable;
myHT->Add( "FIRST", "Hello" );
myHT->Add( "SECOND", "World" );
myHT->Add( "THIRD", "!" );
// Create a SortedList using the default comparer.
SortedList^ mySL1 = gcnew SortedList( myHT );
Console::WriteLine( "mySL1 (default):" );
try
{
mySL1->Add( "first", "Ola!" );
}
catch ( ArgumentException^ e )
{
Console::WriteLine( e );
}
PrintKeysAndValues( mySL1 );
// Create a SortedList using the specified case-insensitive comparer.
SortedList^ mySL2 = gcnew SortedList( myHT,gcnew CaseInsensitiveComparer );
Console::WriteLine( "mySL2 (case-insensitive comparer):" );
try
{
mySL2->Add( "first", "Ola!" );
}
catch ( ArgumentException^ e )
{
Console::WriteLine( e );
}
PrintKeysAndValues( mySL2 );
// Create a SortedList using the specified CaseInsensitiveComparer,
// which is based on the Turkish culture (tr-TR), where "I" is not
// the uppercase version of "i".
CultureInfo^ myCul = gcnew CultureInfo( "tr-TR" );
SortedList^ mySL3 = gcnew SortedList( myHT, gcnew CaseInsensitiveComparer( myCul ) );
Console::WriteLine( "mySL3 (case-insensitive comparer, Turkish culture):" );
try
{
mySL3->Add( "first", "Ola!" );
}
catch ( ArgumentException^ e )
{
Console::WriteLine( e );
}
PrintKeysAndValues( mySL3 );
// Create a SortedList using the ComparisonType.InvariantCultureIgnoreCase value.
SortedList^ mySL4 = gcnew SortedList( myHT, StringComparer::InvariantCultureIgnoreCase );
Console::WriteLine( "mySL4 (InvariantCultureIgnoreCase):" );
try
{
mySL4->Add( "first", "Ola!" );
}
catch ( ArgumentException^ e )
{
Console::WriteLine( e );
}
PrintKeysAndValues( mySL4 );
}
/*
This code produces the following output. Results vary depending on the system's culture settings.
mySL1 (default):
-KEY- -VALUE-
first : Ola!
FIRST : Hello
SECOND: World
THIRD : !
mySL2 (case-insensitive comparer):
System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'
at System.Collections.SortedList.Add(Object key, Object value)
at SamplesSortedList.Main()
-KEY- -VALUE-
FIRST : Hello
SECOND: World
THIRD : !
mySL3 (case-insensitive comparer, Turkish culture):
-KEY- -VALUE-
FIRST : Hello
first : Ola!
SECOND: World
THIRD : !
mySL4 (InvariantCultureIgnoreCase):
System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'
at System.Collections.SortedList.Add(Object key, Object value)
at SamplesSortedList.Main()
-KEY- -VALUE-
FIRST : Hello
SECOND: World
THIRD : !
*/
using System;
using System.Collections;
using System.Globalization;
public class SamplesSortedList
{
public static void Main()
{
// Create the dictionary.
Hashtable myHT = new Hashtable();
myHT.Add("FIRST", "Hello");
myHT.Add("SECOND", "World");
myHT.Add("THIRD", "!");
// Create a SortedList using the default comparer.
SortedList mySL1 = new SortedList(myHT);
Console.WriteLine("mySL1 (default):");
try
{
mySL1.Add("first", "Ola!");
}
catch (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL1);
// Create a SortedList using the specified case-insensitive comparer.
SortedList mySL2 = new SortedList(myHT, new CaseInsensitiveComparer());
Console.WriteLine("mySL2 (case-insensitive comparer):");
try
{
mySL2.Add("first", "Ola!");
}
catch (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL2);
// Create a SortedList using the specified CaseInsensitiveComparer,
// which is based on the Turkish culture (tr-TR), where "I" is not
// the uppercase version of "i".
CultureInfo myCul = new CultureInfo("tr-TR");
SortedList mySL3 = new SortedList(myHT, new CaseInsensitiveComparer(myCul));
Console.WriteLine("mySL3 (case-insensitive comparer, Turkish culture):");
try
{
mySL3.Add("first", "Ola!");
}
catch (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL3);
// Create a SortedList using the
// StringComparer.InvariantCultureIgnoreCase value.
SortedList mySL4 = new SortedList(
myHT, StringComparer.InvariantCultureIgnoreCase);
Console.WriteLine("mySL4 (InvariantCultureIgnoreCase):");
try
{
mySL4.Add("first", "Ola!");
}
catch (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL4);
}
public static void PrintKeysAndValues(SortedList myList)
{
Console.WriteLine(" -KEY- -VALUE-");
for (int i = 0; i < myList.Count; i++)
{
Console.WriteLine(" {0,-6}: {1}",
myList.GetKey(i), myList.GetByIndex(i));
}
Console.WriteLine();
}
}
/*
This code produces the following output. Results vary depending on the system's culture settings.
mySL1 (default):
-KEY- -VALUE-
first : Ola!
FIRST : Hello
SECOND: World
THIRD : !
mySL2 (case-insensitive comparer):
System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'
at System.Collections.SortedList.Add(Object key, Object value)
at SamplesSortedList.Main()
-KEY- -VALUE-
FIRST : Hello
SECOND: World
THIRD : !
mySL3 (case-insensitive comparer, Turkish culture):
-KEY- -VALUE-
FIRST : Hello
first : Ola!
SECOND: World
THIRD : !
mySL4 (InvariantCultureIgnoreCase):
System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'
at System.Collections.SortedList.Add(Object key, Object value)
at SamplesSortedList.Main()
-KEY- -VALUE-
FIRST : Hello
SECOND: World
THIRD : !
*/
Imports System.Collections
Imports System.Globalization
Public Class SamplesSortedList
Public Shared Sub Main()
' Create the dictionary.
Dim myHT As New Hashtable()
myHT.Add("FIRST", "Hello")
myHT.Add("SECOND", "World")
myHT.Add("THIRD", "!")
' Create a SortedList using the default comparer.
Dim mySL1 As New SortedList(myHT)
Console.WriteLine("mySL1 (default):")
Try
mySL1.Add("first", "Ola!")
Catch e As ArgumentException
Console.WriteLine(e)
End Try
PrintKeysAndValues(mySL1)
' Create a SortedList using the specified case-insensitive comparer.
Dim mySL2 As New SortedList(myHT, New CaseInsensitiveComparer())
Console.WriteLine("mySL2 (case-insensitive comparer):")
Try
mySL2.Add("first", "Ola!")
Catch e As ArgumentException
Console.WriteLine(e)
End Try
PrintKeysAndValues(mySL2)
' Create a SortedList using the specified CaseInsensitiveComparer,
' which is based on the Turkish culture (tr-TR), where "I" is not
' the uppercase version of "i".
Dim myCul As New CultureInfo("tr-TR")
Dim mySL3 As New SortedList(myHT, New CaseInsensitiveComparer(myCul))
Console.WriteLine("mySL3 (case-insensitive comparer, Turkish culture):")
Try
mySL3.Add("first", "Ola!")
Catch e As ArgumentException
Console.WriteLine(e)
End Try
PrintKeysAndValues(mySL3)
' Create a SortedList using the
' StringComparer.InvariantCultureIgnoreCase value.
Dim mySL4 As New SortedList(myHT, StringComparer.InvariantCultureIgnoreCase)
Console.WriteLine("mySL4 (InvariantCultureIgnoreCase):")
Try
mySL4.Add("first", "Ola!")
Catch e As ArgumentException
Console.WriteLine(e)
End Try
PrintKeysAndValues(mySL4)
End Sub
Public Shared Sub PrintKeysAndValues(ByVal myList As SortedList)
Console.WriteLine(" -KEY- -VALUE-")
Dim i As Integer
For i = 0 To myList.Count - 1
Console.WriteLine(" {0,-6}: {1}", _
myList.GetKey(i), myList.GetByIndex(i))
Next i
Console.WriteLine()
End Sub
End Class
'This code produces the following output. Results vary depending on the system's culture settings.
'
'mySL1 (default):
' -KEY- -VALUE-
' first : Ola!
' FIRST : Hello
' SECOND: World
' THIRD : !
'
'mySL2 (case-insensitive comparer):
'System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'' at System.Collections.SortedList.Add(Object key, Object value)
' at SamplesSortedList.Main()
' -KEY- -VALUE-
' FIRST : Hello
' SECOND: World
' THIRD : !
'
'mySL3 (case-insensitive comparer, Turkish culture):
' -KEY- -VALUE-
' FIRST : Hello
' first : Ola!
' SECOND: World
' THIRD : !
'
'mySL4 (InvariantCultureIgnoreCase):
'System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'' at System.Collections.SortedList.Add(Object key, Object value)
' at SamplesSortedList.Main()
' -KEY- -VALUE-
' FIRST : Hello
' SECOND: World
' THIRD : !
Açıklamalar
Her anahtar, nesnedeki IComparable diğer tüm anahtarlarla karşılaştırma yapabilmesi için arabirimini SortedList uygulamalıdır. Öğeler, öğesine eklenen her anahtarın uygulamasına SortedListgöre IComparable sıralanır.
Hashtable Nesnesi, bu oluşturucuya geçirilebilen bir IDictionary uygulama örneğidir. Yeni SortedList nesne, anahtarların ve içinde depolanan değerlerin Hashtablebir kopyasını içerir.
Bir SortedList nesnenin kapasitesi, öğesinin barındırabileceği öğe SortedList sayısıdır. öğesine SortedListöğe eklendikçe, iç dizi yeniden konumlandırılarak kapasite gerektiği gibi otomatik olarak artırılır.
Koleksiyonun boyutu tahmin edilebilirse, ilk kapasitenin belirtilmesi, nesneye öğe eklerken bir dizi yeniden boyutlandırma işlemi gerçekleştirme gereksinimini SortedList ortadan kaldırır.
Bu oluşturucu, içindeki d
öğelerin sayısı olan n
bir O(n)
işlemdir.
Ayrıca bkz.
- IDictionary
- IComparable
- Hashtable
- Capacity
- Koleksiyonlarda Kültüre Duyarsız Dize İşlemlerini Gerçekleştirme
Şunlara uygulanır
SortedList(Int32)
- Kaynak:
- SortedList.cs
- Kaynak:
- SortedList.cs
- Kaynak:
- SortedList.cs
Boş olan, belirtilen başlangıç kapasitesine sahip olan ve nesneye eklenen her anahtar tarafından uygulanan arabirime IComparable göre sıralanan yeni bir sınıf örneğini SortedListSortedList başlatır.
public:
SortedList(int initialCapacity);
public SortedList (int initialCapacity);
new System.Collections.SortedList : int -> System.Collections.SortedList
Public Sub New (initialCapacity As Integer)
Parametreler
- initialCapacity
- Int32
Nesnenin SortedList içerebileceği ilk öğe sayısı.
Özel durumlar
initialCapacity
, sıfırdan küçüktür.
Belirtilen initialCapacity
ile bir SortedList nesne oluşturmak için yeterli kullanılabilir bellek yok.
Örnekler
Aşağıdaki kod örneği, farklı SortedList oluşturucuları kullanarak koleksiyonlar oluşturur ve koleksiyonların davranışındaki farkları gösterir.
// The following code example creates SortedList instances using different constructors
// and demonstrates the differences in the behavior of the SortedList instances.
using namespace System;
using namespace System::Collections;
using namespace System::Globalization;
void PrintKeysAndValues( SortedList^ myList )
{
Console::WriteLine( " Capacity is {0}.", myList->Capacity );
Console::WriteLine( " -KEY- -VALUE-" );
for ( int i = 0; i < myList->Count; i++ )
{
Console::WriteLine( " {0,-6}: {1}", myList->GetKey( i ), myList->GetByIndex( i ) );
}
Console::WriteLine();
}
int main()
{
// Create a SortedList using the default comparer.
SortedList^ mySL1 = gcnew SortedList( 3 );
Console::WriteLine( "mySL1 (default):" );
mySL1->Add( "FIRST", "Hello" );
mySL1->Add( "SECOND", "World" );
mySL1->Add( "THIRD", "!" );
try
{
mySL1->Add( "first", "Ola!" );
}
catch ( ArgumentException^ e )
{
Console::WriteLine( e );
}
PrintKeysAndValues( mySL1 );
// Create a SortedList using the specified case-insensitive comparer.
SortedList^ mySL2 = gcnew SortedList( gcnew CaseInsensitiveComparer,3 );
Console::WriteLine( "mySL2 (case-insensitive comparer):" );
mySL2->Add( "FIRST", "Hello" );
mySL2->Add( "SECOND", "World" );
mySL2->Add( "THIRD", "!" );
try
{
mySL2->Add( "first", "Ola!" );
}
catch ( ArgumentException^ e )
{
Console::WriteLine( e );
}
PrintKeysAndValues( mySL2 );
// Create a SortedList using the specified CaseInsensitiveComparer,
// which is based on the Turkish culture (tr-TR), where "I" is not
// the uppercase version of "i".
CultureInfo^ myCul = gcnew CultureInfo("tr-TR");
SortedList^ mySL3 = gcnew SortedList(gcnew CaseInsensitiveComparer(myCul), 3);
Console::WriteLine("mySL3 (case-insensitive comparer, Turkish culture):");
mySL3->Add("FIRST", "Hello");
mySL3->Add("SECOND", "World");
mySL3->Add("THIRD", "!");
try
{
mySL3->Add("first", "Ola!");
}
catch (ArgumentException^ e)
{
Console::WriteLine(e);
}
PrintKeysAndValues(mySL3);
// Create a SortedList using the
// StringComparer.InvariantCultureIgnoreCase value.
SortedList^ mySL4 = gcnew SortedList( StringComparer::InvariantCultureIgnoreCase, 3 );
Console::WriteLine( "mySL4 (InvariantCultureIgnoreCase):" );
mySL4->Add( "FIRST", "Hello" );
mySL4->Add( "SECOND", "World" );
mySL4->Add( "THIRD", "!" );
try
{
mySL4->Add( "first", "Ola!" );
}
catch ( ArgumentException^ e )
{
Console::WriteLine( e );
}
PrintKeysAndValues( mySL4 );
}
/*
This code produces the following output. Results vary depending on the system's culture settings.
mySL1 (default):
Capacity is 6.
-KEY- -VALUE-
first : Ola!
FIRST : Hello
SECOND: World
THIRD : !
mySL2 (case-insensitive comparer):
System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'
at System.Collections.SortedList.Add(Object key, Object value)
at SamplesSortedList.Main()
Capacity is 3.
-KEY- -VALUE-
FIRST : Hello
SECOND: World
THIRD : !
mySL3 (case-insensitive comparer, Turkish culture):
Capacity is 6.
-KEY- -VALUE-
FIRST : Hello
first : Ola!
SECOND: World
THIRD : !
mySL4 (InvariantCultureIgnoreCase):
System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'
at System.Collections.SortedList.Add(Object key, Object value)
at SamplesSortedList.Main()
Capacity is 3.
-KEY- -VALUE-
FIRST : Hello
SECOND: World
THIRD : !
*/
using System;
using System.Collections;
using System.Globalization;
public class SamplesSortedList
{
public static void Main()
{
// Create a SortedList using the default comparer.
SortedList mySL1 = new SortedList( 3 );
Console.WriteLine("mySL1 (default):");
mySL1.Add("FIRST", "Hello");
mySL1.Add("SECOND", "World");
mySL1.Add("THIRD", "!");
try
{
mySL1.Add("first", "Ola!");
}
catch (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL1);
// Create a SortedList using the specified case-insensitive comparer.
SortedList mySL2 = new SortedList(new CaseInsensitiveComparer(), 3);
Console.WriteLine("mySL2 (case-insensitive comparer):");
mySL2.Add("FIRST", "Hello");
mySL2.Add("SECOND", "World");
mySL2.Add("THIRD", "!");
try
{
mySL2.Add("first", "Ola!");
}
catch (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL2);
// Create a SortedList using the specified CaseInsensitiveComparer,
// which is based on the Turkish culture (tr-TR), where "I" is not
// the uppercase version of "i".
CultureInfo myCul = new CultureInfo("tr-TR");
SortedList mySL3 =
new SortedList(new CaseInsensitiveComparer(myCul), 3);
Console.WriteLine(
"mySL3 (case-insensitive comparer, Turkish culture):");
mySL3.Add("FIRST", "Hello");
mySL3.Add("SECOND", "World");
mySL3.Add("THIRD", "!");
try
{
mySL3.Add("first", "Ola!");
}
catch (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL3);
// Create a SortedList using the
// StringComparer.InvariantCultureIgnoreCase value.
SortedList mySL4 = new SortedList(
StringComparer.InvariantCultureIgnoreCase, 3);
Console.WriteLine("mySL4 (InvariantCultureIgnoreCase):");
mySL4.Add("FIRST", "Hello");
mySL4.Add("SECOND", "World");
mySL4.Add("THIRD", "!");
try
{
mySL4.Add("first", "Ola!");
}
catch (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL4);
}
public static void PrintKeysAndValues(SortedList myList)
{
Console.WriteLine(" -KEY- -VALUE-");
for (int i = 0; i < myList.Count; i++)
{
Console.WriteLine(" {0,-6}: {1}",
myList.GetKey(i), myList.GetByIndex(i));
}
Console.WriteLine();
}
}
/*
This code produces the following output.
Results vary depending on the system's culture settings.
mySL1 (default):
-KEY- -VALUE-
first : Ola!
FIRST : Hello
SECOND: World
THIRD : !
mySL2 (case-insensitive comparer):
System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'
at System.Collections.SortedList.Add(Object key, Object value)
at SamplesSortedList.Main()
-KEY- -VALUE-
FIRST : Hello
SECOND: World
THIRD : !
mySL3 (case-insensitive comparer, Turkish culture):
-KEY- -VALUE-
FIRST : Hello
first : Ola!
SECOND: World
THIRD : !
mySL4 (InvariantCultureIgnoreCase):
System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'
at System.Collections.SortedList.Add(Object key, Object value)
at SamplesSortedList.Main()
-KEY- -VALUE-
FIRST : Hello
SECOND: World
THIRD : !
*/
Imports System.Collections
Imports System.Globalization
Public Class SamplesSortedList
Public Shared Sub Main()
' Create a SortedList using the default comparer.
Dim mySL1 As New SortedList( 3 )
Console.WriteLine("mySL1 (default):")
mySL1.Add("FIRST", "Hello")
mySL1.Add("SECOND", "World")
mySL1.Add("THIRD", "!")
Try
mySL1.Add("first", "Ola!")
Catch e As ArgumentException
Console.WriteLine(e)
End Try
PrintKeysAndValues(mySL1)
' Create a SortedList using the specified case-insensitive comparer.
Dim mySL2 As New SortedList(New CaseInsensitiveComparer(), 3)
Console.WriteLine("mySL2 (case-insensitive comparer):")
mySL2.Add("FIRST", "Hello")
mySL2.Add("SECOND", "World")
mySL2.Add("THIRD", "!")
Try
mySL2.Add("first", "Ola!")
Catch e As ArgumentException
Console.WriteLine(e)
End Try
PrintKeysAndValues(mySL2)
' Create a SortedList using the specified CaseInsensitiveComparer,
' which is based on the Turkish culture (tr-TR), where "I" is not
' the uppercase version of "i".
Dim myCul As New CultureInfo("tr-TR")
Dim mySL3 As New SortedList(New CaseInsensitiveComparer(myCul), 3)
Console.WriteLine("mySL3 (case-insensitive comparer, Turkish culture):")
mySL3.Add("FIRST", "Hello")
mySL3.Add("SECOND", "World")
mySL3.Add("THIRD", "!")
Try
mySL3.Add("first", "Ola!")
Catch e As ArgumentException
Console.WriteLine(e)
End Try
PrintKeysAndValues(mySL3)
' Create a SortedList using the
' StringComparer.InvariantCultureIgnoreCase value.
Dim mySL4 As New SortedList( _
StringComparer.InvariantCultureIgnoreCase, 3)
Console.WriteLine("mySL4 (InvariantCultureIgnoreCase):")
mySL4.Add("FIRST", "Hello")
mySL4.Add("SECOND", "World")
mySL4.Add("THIRD", "!")
Try
mySL4.Add("first", "Ola!")
Catch e As ArgumentException
Console.WriteLine(e)
End Try
PrintKeysAndValues(mySL4)
End Sub
Public Shared Sub PrintKeysAndValues(ByVal myList As SortedList)
Console.WriteLine(" -KEY- -VALUE-")
Dim i As Integer
For i = 0 To myList.Count - 1
Console.WriteLine(" {0,-6}: {1}", _
myList.GetKey(i), myList.GetByIndex(i))
Next i
Console.WriteLine()
End Sub
End Class
'This code produces the following output. Results vary depending on the system's culture settings.
'
'mySL1 (default):
' -KEY- -VALUE-
' first : Ola!
' FIRST : Hello
' SECOND: World
' THIRD : !
'
'mySL2 (case-insensitive comparer):
'System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'' at System.Collections.SortedList.Add(Object key, Object value)
' at SamplesSortedList.Main()
' -KEY- -VALUE-
' FIRST : Hello
' SECOND: World
' THIRD : !
'
'mySL3 (case-insensitive comparer, Turkish culture):
' -KEY- -VALUE-
' FIRST : Hello
' first : Ola!
' SECOND: World
' THIRD : !
'
'mySL4 (InvariantCultureIgnoreCase):
'System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'' at System.Collections.SortedList.Add(Object key, Object value)
' at SamplesSortedList.Main()
' -KEY- -VALUE-
' FIRST : Hello
' SECOND: World
' THIRD : !
Açıklamalar
Her anahtar, nesnedeki IComparable diğer tüm anahtarlarla karşılaştırma yapabilmesi için arabirimini SortedList uygulamalıdır. Öğeler, öğesine eklenen her anahtarın uygulamasına SortedListgöre IComparable sıralanır.
Bir SortedList nesnenin kapasitesi, öğesinin barındırabileceği öğe SortedList sayısıdır. öğesine SortedListöğe eklendikçe, iç dizi yeniden konumlandırılarak kapasite gerektiği gibi otomatik olarak artırılır.
Koleksiyonun boyutu tahmin edilebilirse, ilk kapasitenin belirtilmesi, nesneye öğe eklerken bir dizi yeniden boyutlandırma işlemi gerçekleştirme gereksinimini SortedList ortadan kaldırır.
Bu oluşturucu bir O(n)
işlemdir ve burada n
değeridir initialCapacity
.
Ayrıca bkz.
Şunlara uygulanır
SortedList(IComparer, Int32)
- Kaynak:
- SortedList.cs
- Kaynak:
- SortedList.cs
- Kaynak:
- SortedList.cs
Sınıfın SortedList boş, belirtilen başlangıç kapasitesine sahip ve belirtilen IComparer arabirime göre sıralanmış yeni bir örneğini başlatır.
public:
SortedList(System::Collections::IComparer ^ comparer, int capacity);
public SortedList (System.Collections.IComparer comparer, int capacity);
public SortedList (System.Collections.IComparer? comparer, int capacity);
new System.Collections.SortedList : System.Collections.IComparer * int -> System.Collections.SortedList
Public Sub New (comparer As IComparer, capacity As Integer)
Parametreler
- comparer
- IComparer
IComparer Anahtarları karşılaştırırken kullanılacak uygulama.
-veya-
null
her anahtarın IComparable uygulamasını kullanmak için.
- capacity
- Int32
Nesnenin SortedList içerebileceği ilk öğe sayısı.
Özel durumlar
capacity
, sıfırdan küçüktür.
Belirtilen capacity
ile bir SortedList nesne oluşturmak için yeterli kullanılabilir bellek yok.
Örnekler
Aşağıdaki kod örneği, farklı SortedList oluşturucuları kullanarak koleksiyonlar oluşturur ve koleksiyonların davranışındaki farkları gösterir.
// The following code example creates SortedList instances using different constructors
// and demonstrates the differences in the behavior of the SortedList instances.
using namespace System;
using namespace System::Collections;
using namespace System::Globalization;
void PrintKeysAndValues( SortedList^ myList )
{
Console::WriteLine( " Capacity is {0}.", myList->Capacity );
Console::WriteLine( " -KEY- -VALUE-" );
for ( int i = 0; i < myList->Count; i++ )
{
Console::WriteLine( " {0,-6}: {1}", myList->GetKey( i ), myList->GetByIndex( i ) );
}
Console::WriteLine();
}
int main()
{
// Create a SortedList using the default comparer.
SortedList^ mySL1 = gcnew SortedList( 3 );
Console::WriteLine( "mySL1 (default):" );
mySL1->Add( "FIRST", "Hello" );
mySL1->Add( "SECOND", "World" );
mySL1->Add( "THIRD", "!" );
try
{
mySL1->Add( "first", "Ola!" );
}
catch ( ArgumentException^ e )
{
Console::WriteLine( e );
}
PrintKeysAndValues( mySL1 );
// Create a SortedList using the specified case-insensitive comparer.
SortedList^ mySL2 = gcnew SortedList( gcnew CaseInsensitiveComparer,3 );
Console::WriteLine( "mySL2 (case-insensitive comparer):" );
mySL2->Add( "FIRST", "Hello" );
mySL2->Add( "SECOND", "World" );
mySL2->Add( "THIRD", "!" );
try
{
mySL2->Add( "first", "Ola!" );
}
catch ( ArgumentException^ e )
{
Console::WriteLine( e );
}
PrintKeysAndValues( mySL2 );
// Create a SortedList using the specified CaseInsensitiveComparer,
// which is based on the Turkish culture (tr-TR), where "I" is not
// the uppercase version of "i".
CultureInfo^ myCul = gcnew CultureInfo("tr-TR");
SortedList^ mySL3 = gcnew SortedList(gcnew CaseInsensitiveComparer(myCul), 3);
Console::WriteLine("mySL3 (case-insensitive comparer, Turkish culture):");
mySL3->Add("FIRST", "Hello");
mySL3->Add("SECOND", "World");
mySL3->Add("THIRD", "!");
try
{
mySL3->Add("first", "Ola!");
}
catch (ArgumentException^ e)
{
Console::WriteLine(e);
}
PrintKeysAndValues(mySL3);
// Create a SortedList using the
// StringComparer.InvariantCultureIgnoreCase value.
SortedList^ mySL4 = gcnew SortedList( StringComparer::InvariantCultureIgnoreCase, 3 );
Console::WriteLine( "mySL4 (InvariantCultureIgnoreCase):" );
mySL4->Add( "FIRST", "Hello" );
mySL4->Add( "SECOND", "World" );
mySL4->Add( "THIRD", "!" );
try
{
mySL4->Add( "first", "Ola!" );
}
catch ( ArgumentException^ e )
{
Console::WriteLine( e );
}
PrintKeysAndValues( mySL4 );
}
/*
This code produces the following output. Results vary depending on the system's culture settings.
mySL1 (default):
Capacity is 6.
-KEY- -VALUE-
first : Ola!
FIRST : Hello
SECOND: World
THIRD : !
mySL2 (case-insensitive comparer):
System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'
at System.Collections.SortedList.Add(Object key, Object value)
at SamplesSortedList.Main()
Capacity is 3.
-KEY- -VALUE-
FIRST : Hello
SECOND: World
THIRD : !
mySL3 (case-insensitive comparer, Turkish culture):
Capacity is 6.
-KEY- -VALUE-
FIRST : Hello
first : Ola!
SECOND: World
THIRD : !
mySL4 (InvariantCultureIgnoreCase):
System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'
at System.Collections.SortedList.Add(Object key, Object value)
at SamplesSortedList.Main()
Capacity is 3.
-KEY- -VALUE-
FIRST : Hello
SECOND: World
THIRD : !
*/
using System;
using System.Collections;
using System.Globalization;
public class SamplesSortedList
{
public static void Main()
{
// Create a SortedList using the default comparer.
SortedList mySL1 = new SortedList( 3 );
Console.WriteLine("mySL1 (default):");
mySL1.Add("FIRST", "Hello");
mySL1.Add("SECOND", "World");
mySL1.Add("THIRD", "!");
try
{
mySL1.Add("first", "Ola!");
}
catch (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL1);
// Create a SortedList using the specified case-insensitive comparer.
SortedList mySL2 = new SortedList(new CaseInsensitiveComparer(), 3);
Console.WriteLine("mySL2 (case-insensitive comparer):");
mySL2.Add("FIRST", "Hello");
mySL2.Add("SECOND", "World");
mySL2.Add("THIRD", "!");
try
{
mySL2.Add("first", "Ola!");
}
catch (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL2);
// Create a SortedList using the specified CaseInsensitiveComparer,
// which is based on the Turkish culture (tr-TR), where "I" is not
// the uppercase version of "i".
CultureInfo myCul = new CultureInfo("tr-TR");
SortedList mySL3 =
new SortedList(new CaseInsensitiveComparer(myCul), 3);
Console.WriteLine(
"mySL3 (case-insensitive comparer, Turkish culture):");
mySL3.Add("FIRST", "Hello");
mySL3.Add("SECOND", "World");
mySL3.Add("THIRD", "!");
try
{
mySL3.Add("first", "Ola!");
}
catch (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL3);
// Create a SortedList using the
// StringComparer.InvariantCultureIgnoreCase value.
SortedList mySL4 = new SortedList(
StringComparer.InvariantCultureIgnoreCase, 3);
Console.WriteLine("mySL4 (InvariantCultureIgnoreCase):");
mySL4.Add("FIRST", "Hello");
mySL4.Add("SECOND", "World");
mySL4.Add("THIRD", "!");
try
{
mySL4.Add("first", "Ola!");
}
catch (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL4);
}
public static void PrintKeysAndValues(SortedList myList)
{
Console.WriteLine(" -KEY- -VALUE-");
for (int i = 0; i < myList.Count; i++)
{
Console.WriteLine(" {0,-6}: {1}",
myList.GetKey(i), myList.GetByIndex(i));
}
Console.WriteLine();
}
}
/*
This code produces the following output.
Results vary depending on the system's culture settings.
mySL1 (default):
-KEY- -VALUE-
first : Ola!
FIRST : Hello
SECOND: World
THIRD : !
mySL2 (case-insensitive comparer):
System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'
at System.Collections.SortedList.Add(Object key, Object value)
at SamplesSortedList.Main()
-KEY- -VALUE-
FIRST : Hello
SECOND: World
THIRD : !
mySL3 (case-insensitive comparer, Turkish culture):
-KEY- -VALUE-
FIRST : Hello
first : Ola!
SECOND: World
THIRD : !
mySL4 (InvariantCultureIgnoreCase):
System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'
at System.Collections.SortedList.Add(Object key, Object value)
at SamplesSortedList.Main()
-KEY- -VALUE-
FIRST : Hello
SECOND: World
THIRD : !
*/
Imports System.Collections
Imports System.Globalization
Public Class SamplesSortedList
Public Shared Sub Main()
' Create a SortedList using the default comparer.
Dim mySL1 As New SortedList( 3 )
Console.WriteLine("mySL1 (default):")
mySL1.Add("FIRST", "Hello")
mySL1.Add("SECOND", "World")
mySL1.Add("THIRD", "!")
Try
mySL1.Add("first", "Ola!")
Catch e As ArgumentException
Console.WriteLine(e)
End Try
PrintKeysAndValues(mySL1)
' Create a SortedList using the specified case-insensitive comparer.
Dim mySL2 As New SortedList(New CaseInsensitiveComparer(), 3)
Console.WriteLine("mySL2 (case-insensitive comparer):")
mySL2.Add("FIRST", "Hello")
mySL2.Add("SECOND", "World")
mySL2.Add("THIRD", "!")
Try
mySL2.Add("first", "Ola!")
Catch e As ArgumentException
Console.WriteLine(e)
End Try
PrintKeysAndValues(mySL2)
' Create a SortedList using the specified CaseInsensitiveComparer,
' which is based on the Turkish culture (tr-TR), where "I" is not
' the uppercase version of "i".
Dim myCul As New CultureInfo("tr-TR")
Dim mySL3 As New SortedList(New CaseInsensitiveComparer(myCul), 3)
Console.WriteLine("mySL3 (case-insensitive comparer, Turkish culture):")
mySL3.Add("FIRST", "Hello")
mySL3.Add("SECOND", "World")
mySL3.Add("THIRD", "!")
Try
mySL3.Add("first", "Ola!")
Catch e As ArgumentException
Console.WriteLine(e)
End Try
PrintKeysAndValues(mySL3)
' Create a SortedList using the
' StringComparer.InvariantCultureIgnoreCase value.
Dim mySL4 As New SortedList( _
StringComparer.InvariantCultureIgnoreCase, 3)
Console.WriteLine("mySL4 (InvariantCultureIgnoreCase):")
mySL4.Add("FIRST", "Hello")
mySL4.Add("SECOND", "World")
mySL4.Add("THIRD", "!")
Try
mySL4.Add("first", "Ola!")
Catch e As ArgumentException
Console.WriteLine(e)
End Try
PrintKeysAndValues(mySL4)
End Sub
Public Shared Sub PrintKeysAndValues(ByVal myList As SortedList)
Console.WriteLine(" -KEY- -VALUE-")
Dim i As Integer
For i = 0 To myList.Count - 1
Console.WriteLine(" {0,-6}: {1}", _
myList.GetKey(i), myList.GetByIndex(i))
Next i
Console.WriteLine()
End Sub
End Class
'This code produces the following output. Results vary depending on the system's culture settings.
'
'mySL1 (default):
' -KEY- -VALUE-
' first : Ola!
' FIRST : Hello
' SECOND: World
' THIRD : !
'
'mySL2 (case-insensitive comparer):
'System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'' at System.Collections.SortedList.Add(Object key, Object value)
' at SamplesSortedList.Main()
' -KEY- -VALUE-
' FIRST : Hello
' SECOND: World
' THIRD : !
'
'mySL3 (case-insensitive comparer, Turkish culture):
' -KEY- -VALUE-
' FIRST : Hello
' first : Ola!
' SECOND: World
' THIRD : !
'
'mySL4 (InvariantCultureIgnoreCase):
'System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'' at System.Collections.SortedList.Add(Object key, Object value)
' at SamplesSortedList.Main()
' -KEY- -VALUE-
' FIRST : Hello
' SECOND: World
' THIRD : !
Açıklamalar
Öğeler belirtilen IComparer uygulamaya göre sıralanır. comparer
parametresi isenull
IComparable, her anahtarın uygulanması kullanılır; bu nedenle, her anahtarın nesnedeki IComparableSortedList diğer tüm anahtarlarla karşılaştırma yapabilmesi için arabirimi uygulaması gerekir.
Bir SortedList nesnenin kapasitesi, öğesinin barındırabileceği öğe SortedList sayısıdır. öğesine SortedListöğe eklendikçe, iç dizi yeniden konumlandırılarak kapasite gerektiği gibi otomatik olarak artırılır.
Koleksiyonun boyutu tahmin edilebilirse, ilk kapasitenin belirtilmesi, nesneye öğe eklerken bir dizi yeniden boyutlandırma işlemi gerçekleştirme gereksinimini SortedList ortadan kaldırır.
Bu oluşturucu bir O(n)
işlemdir ve burada n
değeridir capacity
.
Ayrıca bkz.
Şunlara uygulanır
SortedList(IDictionary, IComparer)
- Kaynak:
- SortedList.cs
- Kaynak:
- SortedList.cs
- Kaynak:
- SortedList.cs
Belirtilen sözlükten kopyalanan öğeleri içeren sınıfının yeni bir örneğini SortedList başlatır, kopyalanan öğe sayısıyla aynı başlangıç kapasitesine sahiptir ve belirtilen IComparer arabirime göre sıralanır.
public:
SortedList(System::Collections::IDictionary ^ d, System::Collections::IComparer ^ comparer);
public SortedList (System.Collections.IDictionary d, System.Collections.IComparer comparer);
public SortedList (System.Collections.IDictionary d, System.Collections.IComparer? comparer);
new System.Collections.SortedList : System.Collections.IDictionary * System.Collections.IComparer -> System.Collections.SortedList
Public Sub New (d As IDictionary, comparer As IComparer)
Parametreler
IDictionary Yeni SortedList bir nesneye kopyalanacak uygulama.
- comparer
- IComparer
IComparer Anahtarları karşılaştırırken kullanılacak uygulama.
-veya-
null
her anahtarın IComparable uygulamasını kullanmak için.
Özel durumlar
d
, null
değeridir.
comparer
ve null
içindeki d
bir veya daha fazla öğe arabirimini IComparable uygulamaz.
Örnekler
Aşağıdaki kod örneği, farklı SortedList oluşturucuları kullanarak koleksiyonlar oluşturur ve koleksiyonların davranışındaki farkları gösterir.
using namespace System;
using namespace System::Collections;
using namespace System::Globalization;
void PrintKeysAndValues( SortedList^ myList )
{
Console::WriteLine( " -KEY- -VALUE-" );
for ( int i = 0; i < myList->Count; i++ )
{
Console::WriteLine( " {0,-6}: {1}", myList->GetKey( i ), myList->GetByIndex( i ) );
}
Console::WriteLine();
}
int main()
{
// Create the dictionary.
Hashtable^ myHT = gcnew Hashtable;
myHT->Add( "FIRST", "Hello" );
myHT->Add( "SECOND", "World" );
myHT->Add( "THIRD", "!" );
// Create a SortedList using the default comparer.
SortedList^ mySL1 = gcnew SortedList( myHT );
Console::WriteLine( "mySL1 (default):" );
try
{
mySL1->Add( "first", "Ola!" );
}
catch ( ArgumentException^ e )
{
Console::WriteLine( e );
}
PrintKeysAndValues( mySL1 );
// Create a SortedList using the specified case-insensitive comparer.
SortedList^ mySL2 = gcnew SortedList( myHT,gcnew CaseInsensitiveComparer );
Console::WriteLine( "mySL2 (case-insensitive comparer):" );
try
{
mySL2->Add( "first", "Ola!" );
}
catch ( ArgumentException^ e )
{
Console::WriteLine( e );
}
PrintKeysAndValues( mySL2 );
// Create a SortedList using the specified CaseInsensitiveComparer,
// which is based on the Turkish culture (tr-TR), where "I" is not
// the uppercase version of "i".
CultureInfo^ myCul = gcnew CultureInfo( "tr-TR" );
SortedList^ mySL3 = gcnew SortedList( myHT, gcnew CaseInsensitiveComparer( myCul ) );
Console::WriteLine( "mySL3 (case-insensitive comparer, Turkish culture):" );
try
{
mySL3->Add( "first", "Ola!" );
}
catch ( ArgumentException^ e )
{
Console::WriteLine( e );
}
PrintKeysAndValues( mySL3 );
// Create a SortedList using the ComparisonType.InvariantCultureIgnoreCase value.
SortedList^ mySL4 = gcnew SortedList( myHT, StringComparer::InvariantCultureIgnoreCase );
Console::WriteLine( "mySL4 (InvariantCultureIgnoreCase):" );
try
{
mySL4->Add( "first", "Ola!" );
}
catch ( ArgumentException^ e )
{
Console::WriteLine( e );
}
PrintKeysAndValues( mySL4 );
}
/*
This code produces the following output. Results vary depending on the system's culture settings.
mySL1 (default):
-KEY- -VALUE-
first : Ola!
FIRST : Hello
SECOND: World
THIRD : !
mySL2 (case-insensitive comparer):
System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'
at System.Collections.SortedList.Add(Object key, Object value)
at SamplesSortedList.Main()
-KEY- -VALUE-
FIRST : Hello
SECOND: World
THIRD : !
mySL3 (case-insensitive comparer, Turkish culture):
-KEY- -VALUE-
FIRST : Hello
first : Ola!
SECOND: World
THIRD : !
mySL4 (InvariantCultureIgnoreCase):
System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'
at System.Collections.SortedList.Add(Object key, Object value)
at SamplesSortedList.Main()
-KEY- -VALUE-
FIRST : Hello
SECOND: World
THIRD : !
*/
using System;
using System.Collections;
using System.Globalization;
public class SamplesSortedList
{
public static void Main()
{
// Create the dictionary.
Hashtable myHT = new Hashtable();
myHT.Add("FIRST", "Hello");
myHT.Add("SECOND", "World");
myHT.Add("THIRD", "!");
// Create a SortedList using the default comparer.
SortedList mySL1 = new SortedList(myHT);
Console.WriteLine("mySL1 (default):");
try
{
mySL1.Add("first", "Ola!");
}
catch (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL1);
// Create a SortedList using the specified case-insensitive comparer.
SortedList mySL2 = new SortedList(myHT, new CaseInsensitiveComparer());
Console.WriteLine("mySL2 (case-insensitive comparer):");
try
{
mySL2.Add("first", "Ola!");
}
catch (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL2);
// Create a SortedList using the specified CaseInsensitiveComparer,
// which is based on the Turkish culture (tr-TR), where "I" is not
// the uppercase version of "i".
CultureInfo myCul = new CultureInfo("tr-TR");
SortedList mySL3 = new SortedList(myHT, new CaseInsensitiveComparer(myCul));
Console.WriteLine("mySL3 (case-insensitive comparer, Turkish culture):");
try
{
mySL3.Add("first", "Ola!");
}
catch (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL3);
// Create a SortedList using the
// StringComparer.InvariantCultureIgnoreCase value.
SortedList mySL4 = new SortedList(
myHT, StringComparer.InvariantCultureIgnoreCase);
Console.WriteLine("mySL4 (InvariantCultureIgnoreCase):");
try
{
mySL4.Add("first", "Ola!");
}
catch (ArgumentException e)
{
Console.WriteLine(e);
}
PrintKeysAndValues(mySL4);
}
public static void PrintKeysAndValues(SortedList myList)
{
Console.WriteLine(" -KEY- -VALUE-");
for (int i = 0; i < myList.Count; i++)
{
Console.WriteLine(" {0,-6}: {1}",
myList.GetKey(i), myList.GetByIndex(i));
}
Console.WriteLine();
}
}
/*
This code produces the following output. Results vary depending on the system's culture settings.
mySL1 (default):
-KEY- -VALUE-
first : Ola!
FIRST : Hello
SECOND: World
THIRD : !
mySL2 (case-insensitive comparer):
System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'
at System.Collections.SortedList.Add(Object key, Object value)
at SamplesSortedList.Main()
-KEY- -VALUE-
FIRST : Hello
SECOND: World
THIRD : !
mySL3 (case-insensitive comparer, Turkish culture):
-KEY- -VALUE-
FIRST : Hello
first : Ola!
SECOND: World
THIRD : !
mySL4 (InvariantCultureIgnoreCase):
System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'
at System.Collections.SortedList.Add(Object key, Object value)
at SamplesSortedList.Main()
-KEY- -VALUE-
FIRST : Hello
SECOND: World
THIRD : !
*/
Imports System.Collections
Imports System.Globalization
Public Class SamplesSortedList
Public Shared Sub Main()
' Create the dictionary.
Dim myHT As New Hashtable()
myHT.Add("FIRST", "Hello")
myHT.Add("SECOND", "World")
myHT.Add("THIRD", "!")
' Create a SortedList using the default comparer.
Dim mySL1 As New SortedList(myHT)
Console.WriteLine("mySL1 (default):")
Try
mySL1.Add("first", "Ola!")
Catch e As ArgumentException
Console.WriteLine(e)
End Try
PrintKeysAndValues(mySL1)
' Create a SortedList using the specified case-insensitive comparer.
Dim mySL2 As New SortedList(myHT, New CaseInsensitiveComparer())
Console.WriteLine("mySL2 (case-insensitive comparer):")
Try
mySL2.Add("first", "Ola!")
Catch e As ArgumentException
Console.WriteLine(e)
End Try
PrintKeysAndValues(mySL2)
' Create a SortedList using the specified CaseInsensitiveComparer,
' which is based on the Turkish culture (tr-TR), where "I" is not
' the uppercase version of "i".
Dim myCul As New CultureInfo("tr-TR")
Dim mySL3 As New SortedList(myHT, New CaseInsensitiveComparer(myCul))
Console.WriteLine("mySL3 (case-insensitive comparer, Turkish culture):")
Try
mySL3.Add("first", "Ola!")
Catch e As ArgumentException
Console.WriteLine(e)
End Try
PrintKeysAndValues(mySL3)
' Create a SortedList using the
' StringComparer.InvariantCultureIgnoreCase value.
Dim mySL4 As New SortedList(myHT, StringComparer.InvariantCultureIgnoreCase)
Console.WriteLine("mySL4 (InvariantCultureIgnoreCase):")
Try
mySL4.Add("first", "Ola!")
Catch e As ArgumentException
Console.WriteLine(e)
End Try
PrintKeysAndValues(mySL4)
End Sub
Public Shared Sub PrintKeysAndValues(ByVal myList As SortedList)
Console.WriteLine(" -KEY- -VALUE-")
Dim i As Integer
For i = 0 To myList.Count - 1
Console.WriteLine(" {0,-6}: {1}", _
myList.GetKey(i), myList.GetByIndex(i))
Next i
Console.WriteLine()
End Sub
End Class
'This code produces the following output. Results vary depending on the system's culture settings.
'
'mySL1 (default):
' -KEY- -VALUE-
' first : Ola!
' FIRST : Hello
' SECOND: World
' THIRD : !
'
'mySL2 (case-insensitive comparer):
'System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'' at System.Collections.SortedList.Add(Object key, Object value)
' at SamplesSortedList.Main()
' -KEY- -VALUE-
' FIRST : Hello
' SECOND: World
' THIRD : !
'
'mySL3 (case-insensitive comparer, Turkish culture):
' -KEY- -VALUE-
' FIRST : Hello
' first : Ola!
' SECOND: World
' THIRD : !
'
'mySL4 (InvariantCultureIgnoreCase):
'System.ArgumentException: Item has already been added. Key in dictionary: 'FIRST' Key being added: 'first'' at System.Collections.SortedList.Add(Object key, Object value)
' at SamplesSortedList.Main()
' -KEY- -VALUE-
' FIRST : Hello
' SECOND: World
' THIRD : !
Açıklamalar
Öğeler belirtilen IComparer uygulamaya göre sıralanır. comparer
parametresi isenull
IComparable, her anahtarın uygulanması kullanılır; bu nedenle, her anahtarın nesnedeki IComparableSortedList diğer tüm anahtarlarla karşılaştırma yapabilmesi için arabirimi uygulaması gerekir.
Hashtable Nesnesi, bu oluşturucuya geçirilebilen bir IDictionary uygulama örneğidir. Yeni SortedList nesne, anahtarların ve içinde depolanan değerlerin Hashtablebir kopyasını içerir.
Bir SortedList nesnenin kapasitesi, öğesinin barındırabileceği öğe SortedList sayısıdır. öğesine SortedListöğe eklendikçe, iç dizi yeniden konumlandırılarak kapasite gerektiği gibi otomatik olarak artırılır.
Koleksiyonun boyutu tahmin edilebilirse, ilk kapasitenin belirtilmesi, nesneye öğe eklerken bir dizi yeniden boyutlandırma işlemi gerçekleştirme gereksinimini SortedList ortadan kaldırır.
Bu oluşturucu, içindeki d
öğelerin sayısı olan n
bir O(n)
işlemdir.
Ayrıca bkz.
- IDictionary
- IComparer
- IComparable
- Hashtable
- Capacity
- Koleksiyonlarda Kültüre Duyarsız Dize İşlemlerini Gerçekleştirme