SortedList
SortedList Class
키를 기준으로 정렬되고 키와 인덱스로 액세스할 수 있는 키/값 쌍의 컬렉션을 나타냅니다.
속성
Count property
SortedList 개체에 포함된 요소 수를 가져옵니다.
다음 코드는 Count 속성을 사용하는 간단한 예를 보여줍니다.
Sub CountItemsInSortedList()
' Create a new SortedList object
Dim mySL As Object
Set mySL = CreateObject("System.Collections.SortedList")
' Add key-value pairs to the SortedList
mySL.Add "one", "Apple"
mySL.Add "two", "Banana"
mySL.Add "three", "Cherry"
mySL.Add "four", "Durian"
mySL.Add "five", "Elderberry"
mySL.Add "six", "Grape"
mySL.Add "seven", "Loquat"
' Print the count of items in the SortedList
Debug.Print mySL.Count
' Clean up by setting the object to Nothing
Set mySL = Nothing
End Sub
Item property
SortedList 개체의 특정 키와 연관된 값을 가져오거나 설정합니다.
다음 코드는 특정 키의 값을 변경하고 출력하는 간단한 예제입니다.
Sub SortedListExample()
Dim mySL As Object
Set mySL = CreateObject("System.Collections.SortedList")
mySL.Add "one", "Apple"
mySL.Add "two", "Banana"
mySL.Add "three", "Cherry"
mySL.Add "four", "Durian"
mySL.Add "five", "Elderberry"
mySL.Add "six", "Grape"
mySL.Add "seven", "Loquat"
' Get a value using a key.
Debug.Print "'two' is associated with ", mySL.Item("two")
' Change the value of a key.
mySL.Item("two") = "Blueberry"
Debug.Print "'two' is now associated with ", mySL("two")
Set mySL = Nothing
End Sub
' output
' 'two' is associated with Banana
' 'two' is now associated with Blueberry
메서드
Add method
지정한 키와 값을 가진 요소를 SortedList 개체에 추가합니다.
Syntex
object.Add key,value
key : 추가할 요소의 키입니다.
value : 추가할 요소의 값입니다. 값은 null이 될 수 있습니다.
다음 코드는 값을 추가하는 간단한 예제입니다.
Sub SortedListAddExample()
Dim mySL As Object
Set mySL = CreateObject("System.Collections.SortedList")
mySL.Add "one", "Apple"
mySL.Add "two", "Banana"
mySL.Add "three", "Cherry"
mySL.Add "four", "Durian"
mySL.Add "five", "Elderberry"
mySL.Add "six", "Grape"
mySL.Add "seven", "Loquat"
Debug.Print mySL.Count
Set mySL = Nothing
End Sub
Clear method
SortedList 개체에서 요소를 모두 제거합니다.
다음 코드는 Clear 메서드를 이용해서 요소를 모두 제거하는 간단한 예제입니다.
Sub SortedListClearExample()
Dim mySL As Object
Set mySL = CreateObject("System.Collections.SortedList")
mySL.Add "one", "Apple"
mySL.Add "two", "Banana"
mySL.Add "three", "Cherry"
mySL.Add "four", "Durian"
mySL.Add "five", "Elderberry"
mySL.Add "six", "Grape"
mySL.Add "seven", "Loquat"
Debug.Print "Total elements in SortedList:", mySL.Count
mySL.Clear
Debug.Print "Total elements in SortedList after clearing:", mySL.Count
Set mySL = Nothing
End Sub
' output
' Total elements in SortedList: 7
' Total elements in SortedList after clearing: 0
Clone method
컬렉션의 "얕은 복사본" (shallow copy)을 만듭니다.
다음 코드는 Clone 메서드를 사용하는 간단한 예제입니다.
Sub SortedListCloneDemo()
Dim mySL As Object
Dim mySL2 As Object
Dim i As Long
Dim key As Variant
Set mySL = CreateObject("System.Collections.SortedList")
mySL.Add "one", "Apple"
mySL.Add "two", "Banana"
mySL.Add "three", "Cherry"
mySL.Add "four", "Durian"
mySL.Add "five", "Elderberry"
mySL.Add "six", "Grape"
mySL.Add "seven", "Loquat"
Debug.Print "--- Original Sorted List ---"
For i = 0 To mySL.Count - 1
Debug.Print mySL.GetByIndex(i)
Next
Set mySL2 = mySL.Clone
mySL("one") = "Avocado"
Debug.Print "--- Modified Original Sorted List ---"
For i = 0 To mySL.Count - 1
Debug.Print mySL.GetByIndex(i)
Next
Debug.Print "--- Cloned Sorted List after modifying original --- "
For i = 0 To mySL2.Count - 1
Debug.Print mySL2.GetByIndex(i)
Next
Set mySL = Nothing
Set mySL2 = Nothing
End Sub
' output
' --- Original Sorted List ---
' Elderberry
' Durian
' Apple
' Loquat
' Grape
' Cherry
' Banana
' --- Modified Original Sorted List ---
' Elderberry
' Durian
' Avocado
' Loquat
' Grape
' Cherry
' Banana
' --- Cloned Sorted List after modifying original ---
' Elderberry
' Durian
' Apple
' Loquat
' Grape
' Cherry
' Banana
Contains method
SortedList 개체에 특정 키가 포함되어 있는지 여부를 확인합니다.
Syntax
object.Contains (key)
key : SortedList 개체에서 찾을 키입니다.
다음 코드는 특정 키가 포함되어 있는지 확인하는 간단한 예제입니다.
Sub SortedListContainsDemo()
Dim mySL As Object
Set mySL = CreateObject("System.Collections.SortedList")
mySL.Add "one", "Apple"
mySL.Add "two", "Banana"
mySL.Add "three", "Cherry"
mySL.Add "four", "Durian"
mySL.Add "five", "Elderberry"
mySL.Add "six", "Grape"
mySL.Add "seven", "Loquat"
If mySL.Contains("one") Then
Debug.Print "'one' is in the SortedList."
Else
Debug.Print "'one' is not in the SortedList."
End If
Set mySL = Nothing
End Sub
' output
' 'one' is in the SortedList.
ContainsKey method
SortedList 개체에 특정 키가 포함되어 있는지 여부를 확인합니다.
Syntax
object.ContainsKey (key)
key : SortedList 개체에서 찾을 키입니다.
도움말에는 Contains 메서드와 같은 동작을 수행한다고 되어있어서 따로 예제를 만들진 않았습니다.
ContainsValue method
SortedList 개체에 특정 값이 포함되어 있는지 여부를 확인합니다.
Syntax
object.ContainsValue (value)
SortedList 개체에서 찾을 값입니다. 값은 null이 될 수 있습니다.
반환 값
Boolean
SortedList 개체에 지정된 value를 가진 요소가 포함되어 있으면 true이고, 그렇지 않으면 false입니다.
다음 코드는 ContainsValue를 사용하는 간단한 예를 보여줍니다.
Sub SortedListContainsValueExample()
' Create a new SortedList object
Dim mySL As Object
Set mySL = CreateObject("System.Collections.SortedList")
' Add key-value pairs to the SortedList
mySL.Add "one", "Apple"
mySL.Add "two", "Banana"
mySL.Add "three", "Cherry"
mySL.Add "four", "Durian"
mySL.Add "five", "Elderberry"
mySL.Add "six", "Grape"
mySL.Add "seven", "Loquat"
' Check if a specific value is in the SortedList
If mySL.ContainsValue("Apple") Then
Debug.Print "'Apple' is in the SortedList."
Else
Debug.Print "'Apple' is not in the SortedList."
End If
' Clean up by setting the object to Nothing
Set mySL = Nothing
End Sub
' output
' 'Apple' is in the SortedList.
GetByIndex method
SortedList 개체의 지정한 인덱스에서 값을 가져옵니다.
Syntax
object.GetByIndex (index)
가져올 값의 0부터 시작하는 인덱스입니다.
다음 코드는 GetByIndex 메서드를 사용하는 간단한 예제입니다.
Sub SortedListGetByIndexExample()
' Create a new SortedList object
Dim mySL As Object
Dim myIndex As Long
Set mySL = CreateObject("System.Collections.SortedList")
' Add key-value pairs to the SortedList
mySL.Add "one", "Apple"
mySL.Add "two", "Banana"
mySL.Add "three", "Cherry"
mySL.Add "four", "Durian"
mySL.Add "five", "Elderberry"
mySL.Add "six", "Grape"
mySL.Add "seven", "Loquat"
' Define the index
myIndex = 3
' Print the value at the specified index in the SortedList
Debug.Print "The value at index '" & CStr(myIndex) & "' is: '" & CStr(mySL.GetByIndex(myIndex)) & "'."
' Clean up by setting the object to Nothing
Set mySL = Nothing
End Sub
' output
' The value at index '3' is: 'Loquat'.
GetKey method
SortedList 개체의 지정한 인덱스에서 키를 가져옵니다.
Syntax
object.GetKey (index)
가져올 키의 0부터 시작하는 인덱스입니다.
다음 코드는 GetKey 메서드를 사용하는 간단한 예제입니다.
Sub SortedListGetKeyExample()
' Create a new SortedList object
Dim mySL As Object
Dim myIndex As Long
Set mySL = CreateObject("System.Collections.SortedList")
' Add key-value pairs to the SortedList
mySL.Add "one", "Apple"
mySL.Add "two", "Banana"
mySL.Add "three", "Cherry"
mySL.Add "four", "Durian"
mySL.Add "five", "Elderberry"
mySL.Add "six", "Grape"
mySL.Add "seven", "Loquat"
' Define the index
myIndex = 3
' Print the key at the specified index in the SortedList
Debug.Print "The key at index '" & CStr(myIndex) & "' is: '" & CStr(mySL.GetKey(myIndex)) & "'."
' Clean up by setting the object to Nothing
Set mySL = Nothing
End Sub
' output
' The key at index '3' is: 'seven'.
GetKeyList method
SortedList 개체의 키를 가져옵니다.
GetKey 메서드와 비슷한 역할을 하지만 한번에 모든 요소의 키를 가져온다는 점이 다릅니다.
다음 코드는 GetKeyList를 사용하는 간단한 예제입니다.
Sub SortedListGetKeyListExample()
Dim myAL As Object
Dim mySL As Object
Dim i As Long
Dim myIndex As Long
Set myAL = CreateObject("System.Collections.ArrayList")
Set mySL = CreateObject("System.Collections.SortedList")
' Add key-value pairs to the SortedList
mySL.Add "one", "Apple"
mySL.Add "two", "Banana"
mySL.Add "three", "Cherry"
mySL.Add "four", "Durian"
mySL.Add "five", "Elderberry"
mySL.Add "six", "Grape"
mySL.Add "seven", "Loquat"
' Define the index
myIndex = 3
Debug.Print "The key at index '" & CStr(myIndex) & "' is: '" & CStr(mySL.GetKeyList()(myIndex))
' Add all keys from the SortedList to the ArrayList
myAL.AddRange mySL.GetKeyList()
Debug.Print ("All keys in ArrayList:")
For i = 0 To (myAL.Count - 1)
Debug.Print (" Index: '" & CStr(i) & "', Key: '" & CStr(myAL.Item(i)) & "'")
Next i
' Clean up by setting objects to Nothing
Set myAL = Nothing
Set mySL = Nothing
End Sub
' output
' The key at index '3' is: 'seven'
' All keys in ArrayList:
' Index: '0', Key: 'five'
' Index: '1', Key: 'four'
' Index: '2', Key: 'one'
' Index: '3', Key: 'seven'
' Index: '4', Key: 'six'
' Index: '5', Key: 'three'
' Index: '6', Key: 'two'
GetValueList method
SortedList 개체의 값을 가져옵니다.
GetByIndex와 비슷하지만 한번에 모든 요소의 값을 가져온다는 점이 다릅니다.
다음 코드는 GetValueList를 사용하는 간단한 예제입니다.
Sub SortedListGetValueListExample()
Dim myAL As Object
Dim mySL As Object
Dim i As Long
Dim myIndex As Long
Set myAL = CreateObject("System.Collections.ArrayList")
Set mySL = CreateObject("System.Collections.SortedList")
mySL.Add "one", "Apple"
mySL.Add "two", "Banana"
mySL.Add "three", "Cherry"
mySL.Add "four", "Durian"
mySL.Add "five", "Elderberry"
mySL.Add "six", "Grape"
mySL.Add "seven", "Loquat"
myIndex = 3
Debug.Print "The value at index '" & CStr(myIndex) & "' is: '" & CStr(mySL.GetValueList()(myIndex))
myAL.AddRange mySL.GetValueList()
Debug.Print ("All keys in ArrayList:")
For i = 0 To (myAL.Count - 1)
Debug.Print (" Index: '" & CStr(i) & "', Value: '" & CStr(myAL.Item(i)) & "'")
Next i
Set myAL = Nothing
Set mySL = Nothing
End Sub
' output
' The value at index '3' is: 'Loquat
' All keys in ArrayList:
' Index: '0', Value: 'Elderberry'
' Index: '1', Value: 'Durian'
' Index: '2', Value: 'Apple'
' Index: '3', Value: 'Loquat'
' Index: '4', Value: 'Grape'
' Index: '5', Value: 'Cherry'
' Index: '6', Value: 'Banana'
IndexOfKey method
SortedList 개체의 지정된 키 인덱스(0부터 시작)를 반환합니다.
Syntax
object.IndexOfKey (key)
key : SortedList 개체에서 찾을 키입니다.
다음 코드는 특정 키의 인덱스를 찾아서 값을 출력하는 예제입니다.
Sub SortedListIndexOfKeyExample()
Dim mySL As Object
Set mySL = CreateObject("System.Collections.SortedList")
mySL.Add "one", "Apple"
mySL.Add "two", "Banana"
mySL.Add "three", "Cherry"
mySL.Add "four", "Durian"
mySL.Add "five", "Elderberry"
mySL.Add "six", "Grape"
mySL.Add "seven", "Loquat"
' Find the index of a key.
Dim index As Integer
index = mySL.IndexOfKey("two")
If index <> -1 Then
Debug.Print "The index of 'two' is ", index
Debug.Print "'two' is ", mySL.GetByIndex(index)
Else
Debug.Print "'two' is not in the list."
End If
Set mySL = Nothing
End Sub
' output
' 'The index of 'two' is 6
' 'two' is Banana
IndexOfValue method
지정한 값이 SortedList 개체에서 맨 처음 발견되는 인덱스(0부터 시작)를 반환합니다.
Syntax
object.IndexOfValue (value)
value : SortedList 개체에서 찾을 값입니다. 값은 null이 될 수 있습니다.
다음 코드는 특정 값의 인덱스를 찾아서 키를 출력하는 예제입니다.
Sub SortedListIndexOfValueExample()
Dim mySL As Object
Set mySL = CreateObject("System.Collections.SortedList")
mySL.Add "one", "Apple"
mySL.Add "two", "Banana"
mySL.Add "three", "Cherry"
mySL.Add "four", "Durian"
mySL.Add "five", "Elderberry"
mySL.Add "six", "Grape"
mySL.Add "seven", "Loquat"
' Find the index of a value.
Dim index As Integer
index = mySL.IndexOfValue("Banana")
If index <> -1 Then
Debug.Print "The index of 'Banana' is ", index
Debug.Print "'Banana' is ", mySL.GetKey(index)
Else
Debug.Print "'Banana' is not in the list."
End If
Set mySL = Nothing
End Sub
' output
' The index of 'Banana' is 6
' Banana' is two
Remove method
SortedList 개체에서 지정된 키를 가진 요소를 제거합니다.
Syntax
object.Remove (key)
key : 제거할 요소의 키입니다.
다음은 Remove 메서드를 사용하는 간단한 예제입니다.
Sub SortedListRemoveExample()
Dim mySL As Object
Dim i As Long
Set mySL = CreateObject("System.Collections.SortedList")
mySL.Add "one", "Apple"
mySL.Add "two", "Banana"
mySL.Add "three", "Cherry"
mySL.Add "four", "Durian"
mySL.Add "five", "Elderberry"
mySL.Add "six", "Grape"
mySL.Add "seven", "Loquat"
' Print the original list.
Debug.Print ("Original List:")
Call PrintSortedList(mySL)
' Remove an item from the list using a key.
mySL.Remove ("two")
' Print the modified list.
Debug.Print (vbCrLf & "Modified List:")
Call PrintSortedList(mySL)
Set mySL = Nothing
End Sub
Sub PrintSortedList(ByVal sortedList As Object)
Dim i As Long
For i = 0 To sortedList.Count - 1
Debug.Print sortedList.GetKey(i) & "' is associated with '" & sortedList.GetByIndex(i) & "'."
Next
End Sub
' output
' Original List:
' five is associated with Elderberry
' four is associated with Durian
' one is associated with Apple
' seven is associated with Loquat
' six is associated with Grape
' three is associated with Cherry
' two is associated with Banana
' Modified List:
' five is associated with Elderberry
' four is associated with Durian
' one is associated with Apple
' seven is associated with Loquat
' six is associated with Grape
' three is associated with Cherry
RemoveAt method
SortedList 개체의 지정한 인덱스에서 요소를 제거합니다.
Syntax
object.RemoveAt (index)
index : 제거할 요소의 인덱스(0부터 시작)입니다.
Sub SortedListRemoveAtExample()
Dim mySL As Object
Dim i As Long
Set mySL = CreateObject("System.Collections.SortedList")
mySL.Add "one", "Apple"
mySL.Add "two", "Banana"
mySL.Add "three", "Cherry"
mySL.Add "four", "Durian"
mySL.Add "five", "Elderberry"
mySL.Add "six", "Grape"
mySL.Add "seven", "Loquat"
' Print the original list.
Debug.Print ("Original List:")
Call PrintSortedList(mySL)
' Remove an item from the list using an index.
mySL.RemoveAt (1)
' Print the modified list.
Debug.Print (vbCrLf & "Modified List:")
Call PrintSortedList(mySL)
Set mySL = Nothing
End Sub
Sub PrintSortedList(ByVal sortedList As Object)
Dim i As Long
For i = 0 To sortedList.Count - 1
Debug.Print sortedList.GetKey(i) & "' is associated with '" & sortedList.GetByIndex(i) & "'."
Next
End Sub
' output
' Original List:
' five ' is associated with 'Elderberry'.
' four ' is associated with 'Durian'.
' one ' is associated with 'Apple'.
' seven ' is associated with 'Loquat'.
' six ' is associated with 'Grape'.
' three ' is associated with 'Cherry'.
' two ' is associated with 'Banana'.
' Modified List:
' five ' is associated with 'Elderberry'.
' one ' is associated with 'Apple'.
' seven ' is associated with 'Loquat'.
' six ' is associated with 'Grape'.
' three ' is associated with 'Cherry'.
' two ' is associated with 'Banana'.
SetByIndex method
SortedList 개체의 지정한 인덱스에서 값을 바꿉니다.
Syntax
object.SetByIndex (index, value)
index : value를 저장할 0부터 시작하는 인덱스입니다.
value : Object 개체에 저장할 SortedList입니다. 값은 null이 될 수 있습니다.
Sub SortedListSetByIndexExample()
Dim mySL As Object
Dim i As Long
Set mySL = CreateObject("System.Collections.SortedList")
mySL.Add "one", "Apple"
mySL.Add "two", "Banana"
mySL.Add "three", "Cherry"
mySL.Add "four", "Durian"
mySL.Add "five", "Elderberry"
mySL.Add "six", "Grape"
mySL.Add "seven", "Loquat"
' Print the original list.
Debug.Print ("Original List:")
Call PrintSortedList(mySL)
' Set a new value at index 1.
mySL.SetByIndex 1, "Blueberry"
' Print the modified list.
Debug.Print (vbCrLf & "Modified List:")
Call PrintSortedList(mySL)
Set mySL = Nothing
End Sub
Sub PrintSortedList(ByVal sortedList As Object)
Dim i As Long
For i = 0 To sortedList.Count - 1
Debug.Print sortedList.GetKey(i) & "' is associated with '" & sortedList.GetByIndex(i) & "'."
Next
End Sub
' output
' Original List:
' five ' is associated with 'Elderberry'.
' four ' is associated with 'Durian'.
' one ' is associated with 'Apple'.
' seven ' is associated with 'Loquat'.
' six ' is associated with 'Grape'.
' three ' is associated with 'Cherry'.
' two ' is associated with 'Banana'.
' Modified List:
' five ' is associated with 'Elderberry'.
' four ' is associated with 'Blueberry'.
' one ' is associated with 'Apple'.
' seven ' is associated with 'Loquat'.
' six ' is associated with 'Grape'.
' three ' is associated with 'Cherry'.
' two ' is associated with 'Banana'.
도움말 출처
SortedList 클래스 (System.Collections)
키를 기준으로 정렬되고 키와 인덱스로 액세스할 수 있는 키/값 쌍의 컬렉션을 나타냅니다.
learn.microsoft.com