SortedList 생성자
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
SortedList 클래스의 새 인스턴스를 초기화합니다.
오버로드
| Name | Description |
|---|---|
| SortedList() |
비어 있고, 기본 초기 용량을 가지며, 개체에 추가된 각 키가 구현하는 인터페이스에 따라 SortedList 정렬되는 클래스의 새 인스턴스 IComparable 를 SortedList 초기화합니다. |
| SortedList(IComparer) |
비어 있고 기본 초기 용량을 가지며 지정된 SortedList 인터페이스에 따라 정렬되는 클래스의 새 인스턴스 IComparer 를 초기화합니다. |
| SortedList(IDictionary) |
지정된 사전에서 복사한 요소가 들어 있고 복사된 요소 수 SortedList 와 동일한 초기 용량을 가지며 각 키에서 구현하는 인터페이스에 따라 IComparable 정렬되는 클래스의 새 인스턴스를 초기화합니다. |
| SortedList(Int32) |
비어 있고, 지정된 초기 용량을 가지며, 개체에 추가된 각 키가 구현하는 인터페이스에 따라 SortedList 정렬되는 클래스의 새 인스턴스 IComparable 를 SortedList 초기화합니다. |
| SortedList(IComparer, Int32) |
비어 있고, 지정된 초기 용량을 가지며, 지정된 인터페이스에 따라 정렬되는 클래스의 새 인스턴스 SortedList 를 IComparer 초기화합니다. |
| SortedList(IDictionary, IComparer) |
지정된 사전에서 복사한 요소가 들어 있고 복사된 요소 수 SortedList 와 동일한 초기 용량을 가지며 지정된 IComparer 인터페이스에 따라 정렬되는 클래스의 새 인스턴스를 초기화합니다. |
SortedList()
비어 있고, 기본 초기 용량을 가지며, 개체에 추가된 각 키가 구현하는 인터페이스에 따라 SortedList 정렬되는 클래스의 새 인스턴스 IComparable 를 SortedList 초기화합니다.
public:
SortedList();
public SortedList();
Public Sub New ()
예제
다음 코드 예제에서는 다른 SortedList 생성자를 사용 하 여 컬렉션을 만들고 컬렉션의 동작의 차이점을 보여 줍니다.
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 : !
설명
각 키는 개체의 IComparable 다른 모든 키와 비교할 수 있도록 인터페이스를 SortedList 구현해야 합니다. 요소는 에 추가된 각 키의 구현에 IComparable 따라 정렬됩니다 SortedList.
개체의 SortedList 용량은 보유할 수 있는 SortedList 요소의 수입니다. 요소가 추가 SortedList되면 내부 배열을 다시 할당하여 필요에 따라 용량이 자동으로 증가합니다.
컬렉션 크기를 예측할 수 있는 경우 초기 용량을 지정하면 개체에 요소를 SortedList 추가하는 동안 많은 크기 조정 작업을 수행할 필요가 없습니다.
이 생성자는 작업입니다 O(1) .
추가 정보
적용 대상
SortedList(IComparer)
비어 있고 기본 초기 용량을 가지며 지정된 SortedList 인터페이스에 따라 정렬되는 클래스의 새 인스턴스 IComparer 를 초기화합니다.
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)
매개 변수
예제
다음 코드 예제에서는 다른 SortedList 생성자를 사용 하 여 컬렉션을 만들고 컬렉션의 동작의 차이점을 보여 줍니다.
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 : !
설명
요소는 지정된 구현에 따라 정렬됩니다 IComparer . 매개 변수인 comparernullIComparable 경우 각 키의 구현이 사용되므로 각 키는 개체의 다른 모든 키와 비교할 수 있도록 인터페이스를 SortedList 구현 IComparable 해야 합니다.
개체의 SortedList 용량은 보유할 수 있는 SortedList 요소의 수입니다. 요소가 추가 SortedList되면 내부 배열을 다시 할당하여 필요에 따라 용량이 자동으로 증가합니다.
컬렉션 크기를 예측할 수 있는 경우 초기 용량을 지정하면 개체에 요소를 SortedList 추가하는 동안 많은 크기 조정 작업을 수행할 필요가 없습니다.
이 생성자는 작업입니다 O(1) .
추가 정보
적용 대상
SortedList(IDictionary)
지정된 사전에서 복사한 요소가 들어 있고 복사된 요소 수 SortedList 와 동일한 초기 용량을 가지며 각 키에서 구현하는 인터페이스에 따라 IComparable 정렬되는 클래스의 새 인스턴스를 초기화합니다.
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)
매개 변수
IDictionary 새 SortedList 개체에 복사할 구현입니다.
예외
d은 null입니다.
하나 이상의 요소가 d 인터페이스를 IComparable 구현하지 않습니다.
예제
다음 코드 예제에서는 다른 SortedList 생성자를 사용 하 여 컬렉션을 만들고 컬렉션의 동작의 차이점을 보여 줍니다.
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 : !
설명
각 키는 개체의 IComparable 다른 모든 키와 비교할 수 있도록 인터페이스를 SortedList 구현해야 합니다. 요소는 에 추가된 각 키의 구현에 IComparable 따라 정렬됩니다 SortedList.
Hashtable 개체는 이 생성자에 전달할 수 있는 구현의 IDictionary 예입니다. 새 SortedList 개체는 에 저장된 키 및 값의 복사본을 Hashtable포함합니다.
개체의 SortedList 용량은 보유할 수 있는 SortedList 요소의 수입니다. 요소가 추가 SortedList되면 내부 배열을 다시 할당하여 필요에 따라 용량이 자동으로 증가합니다.
컬렉션 크기를 예측할 수 있는 경우 초기 용량을 지정하면 개체에 요소를 SortedList 추가하는 동안 많은 크기 조정 작업을 수행할 필요가 없습니다.
이 생성자는 O(n) 작업의 n 요소 수 d입니다.
추가 정보
적용 대상
SortedList(Int32)
비어 있고, 지정된 초기 용량을 가지며, 개체에 추가된 각 키가 구현하는 인터페이스에 따라 SortedList 정렬되는 클래스의 새 인스턴스 IComparable 를 SortedList 초기화합니다.
public:
SortedList(int initialCapacity);
public SortedList(int initialCapacity);
new System.Collections.SortedList : int -> System.Collections.SortedList
Public Sub New (initialCapacity As Integer)
매개 변수
- initialCapacity
- Int32
개체에 포함될 수 있는 SortedList 초기 요소 수입니다.
예외
initialCapacity가 0보다 작습니다.
지정된 initialCapacity개체를 만드는 데 사용할 수 있는 SortedList 메모리가 부족합니다.
예제
다음 코드 예제에서는 다른 SortedList 생성자를 사용 하 여 컬렉션을 만들고 컬렉션의 동작의 차이점을 보여 줍니다.
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 : !
설명
각 키는 개체의 IComparable 다른 모든 키와 비교할 수 있도록 인터페이스를 SortedList 구현해야 합니다. 요소는 에 추가된 각 키의 구현에 IComparable 따라 정렬됩니다 SortedList.
개체의 SortedList 용량은 보유할 수 있는 SortedList 요소의 수입니다. 요소가 추가 SortedList되면 내부 배열을 다시 할당하여 필요에 따라 용량이 자동으로 증가합니다.
컬렉션 크기를 예측할 수 있는 경우 초기 용량을 지정하면 개체에 요소를 SortedList 추가하는 동안 많은 크기 조정 작업을 수행할 필요가 없습니다.
이 생성자는 O(n) 작업입니다 ninitialCapacity.
추가 정보
적용 대상
SortedList(IComparer, Int32)
비어 있고, 지정된 초기 용량을 가지며, 지정된 인터페이스에 따라 정렬되는 클래스의 새 인스턴스 SortedList 를 IComparer 초기화합니다.
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)
매개 변수
- capacity
- Int32
개체에 포함될 수 있는 SortedList 초기 요소 수입니다.
예외
capacity가 0보다 작습니다.
지정된 capacity개체를 만드는 데 사용할 수 있는 SortedList 메모리가 부족합니다.
예제
다음 코드 예제에서는 다른 SortedList 생성자를 사용 하 여 컬렉션을 만들고 컬렉션의 동작의 차이점을 보여 줍니다.
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 : !
설명
요소는 지정된 구현에 따라 정렬됩니다 IComparer . 매개 변수인 comparernullIComparable 경우 각 키의 구현이 사용되므로 각 키는 개체의 다른 모든 키와 비교할 수 있도록 인터페이스를 SortedList 구현 IComparable 해야 합니다.
개체의 SortedList 용량은 보유할 수 있는 SortedList 요소의 수입니다. 요소가 추가 SortedList되면 내부 배열을 다시 할당하여 필요에 따라 용량이 자동으로 증가합니다.
컬렉션 크기를 예측할 수 있는 경우 초기 용량을 지정하면 개체에 요소를 SortedList 추가하는 동안 많은 크기 조정 작업을 수행할 필요가 없습니다.
이 생성자는 O(n) 작업입니다 ncapacity.
추가 정보
적용 대상
SortedList(IDictionary, IComparer)
지정된 사전에서 복사한 요소가 들어 있고 복사된 요소 수 SortedList 와 동일한 초기 용량을 가지며 지정된 IComparer 인터페이스에 따라 정렬되는 클래스의 새 인스턴스를 초기화합니다.
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)
매개 변수
IDictionary 새 SortedList 개체에 복사할 구현입니다.
예외
d은 null입니다.
comparer 이 null고 하나 이상의 요소가 d 인터페이스를 IComparable 구현하지 않습니다.
예제
다음 코드 예제에서는 다른 SortedList 생성자를 사용 하 여 컬렉션을 만들고 컬렉션의 동작의 차이점을 보여 줍니다.
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 : !
설명
요소는 지정된 구현에 따라 정렬됩니다 IComparer . 매개 변수인 comparernullIComparable 경우 각 키의 구현이 사용되므로 각 키는 개체의 다른 모든 키와 비교할 수 있도록 인터페이스를 SortedList 구현 IComparable 해야 합니다.
Hashtable 개체는 이 생성자에 전달할 수 있는 구현의 IDictionary 예입니다. 새 SortedList 개체는 에 저장된 키 및 값의 복사본을 Hashtable포함합니다.
개체의 SortedList 용량은 보유할 수 있는 SortedList 요소의 수입니다. 요소가 추가 SortedList되면 내부 배열을 다시 할당하여 필요에 따라 용량이 자동으로 증가합니다.
컬렉션 크기를 예측할 수 있는 경우 초기 용량을 지정하면 개체에 요소를 SortedList 추가하는 동안 많은 크기 조정 작업을 수행할 필요가 없습니다.
이 생성자는 O(n) 작업의 n 요소 수 d입니다.