字典来保证顺序和按索引获取



我想要未来的字典,它将以<key><value>的方式存储值;保证顺序是指添加到列表中的每个新项目都作为下一个项目插入到末尾。我读到字典不是好的选择,因此我看了一下OrderedDictionary。此外,我还希望有可能按索引(不是键(获取项目。

的主要问题是我是否选择了 OrderedList 作为我 应该选择吗?

尽管如此,我正在努力按索引获取项目。查看以下内容以了解问题:

Dim _finalList As New OrderedDictionary
_finalList.Add("John", "Miles")
_finalList.Add("Jessica", "Brown")

现在我尝试的以下方法都不起作用:

_finalList.Cast.ElementAt(0))
_finalList.Cast.ElementAt(0).Key.ToString())
_finalList.Cast(Of DictionaryEntry).ElementAt(0))
_finalList.Cast(Of DictionaryEntry).ElementAt(0).Key.ToString())

我得到的错误是这样的:

no public Cast member for the Ordered Dictionary type.

我建议你完成这个实现:

Public Class MyKeyedCollection(Of K, V)
    Implements IDictionary(Of K, V)
    Private lookup = New Dictionary(Of K, V)
    Private items = New List(Of KeyValuePair(Of K, V))
    Default Public Property Item(index As Integer) As V
        Get
            Throw New NotImplementedException()
        End Get
        Set(value As V)
            Throw New NotImplementedException()
        End Set
    End Property
    Default Public Property Item(key As K) As V Implements IDictionary(Of K, V).Item
        Get
            Throw New NotImplementedException()
        End Get
        Set(value As V)
            Throw New NotImplementedException()
        End Set
    End Property
    Public ReadOnly Property Keys As ICollection(Of K) Implements IDictionary(Of K, V).Keys
        Get
            Throw New NotImplementedException()
        End Get
    End Property
    Public ReadOnly Property Values As ICollection(Of V) Implements IDictionary(Of K, V).Values
        Get
            Throw New NotImplementedException()
        End Get
    End Property
    Public ReadOnly Property Count As Integer Implements ICollection(Of KeyValuePair(Of K, V)).Count
        Get
            Throw New NotImplementedException()
        End Get
    End Property
    Public ReadOnly Property IsReadOnly As Boolean Implements ICollection(Of KeyValuePair(Of K, V)).IsReadOnly
        Get
            Throw New NotImplementedException()
        End Get
    End Property
    Public Sub Add(key As K, value As V) Implements IDictionary(Of K, V).Add
        Throw New NotImplementedException()
    End Sub
    Public Sub Add(item As KeyValuePair(Of K, V)) Implements ICollection(Of KeyValuePair(Of K, V)).Add
        Throw New NotImplementedException()
    End Sub
    Public Sub Clear() Implements ICollection(Of KeyValuePair(Of K, V)).Clear
        Throw New NotImplementedException()
    End Sub
    Public Sub CopyTo(array() As KeyValuePair(Of K, V), arrayIndex As Integer) Implements ICollection(Of KeyValuePair(Of K, V)).CopyTo
        Throw New NotImplementedException()
    End Sub
    Public Function ContainsKey(key As K) As Boolean Implements IDictionary(Of K, V).ContainsKey
        Throw New NotImplementedException()
    End Function
    Public Function Remove(key As K) As Boolean Implements IDictionary(Of K, V).Remove
        Throw New NotImplementedException()
    End Function
    Public Function Remove(item As KeyValuePair(Of K, V)) As Boolean Implements ICollection(Of KeyValuePair(Of K, V)).Remove
        Throw New NotImplementedException()
    End Function
    Public Function TryGetValue(key As K, ByRef value As V) As Boolean Implements IDictionary(Of K, V).TryGetValue
        Throw New NotImplementedException()
    End Function
    Public Function Contains(item As KeyValuePair(Of K, V)) As Boolean Implements ICollection(Of KeyValuePair(Of K, V)).Contains
        Throw New NotImplementedException()
    End Function
    Public Function GetEnumerator() As IEnumerator(Of KeyValuePair(Of K, V)) Implements IEnumerable(Of KeyValuePair(Of K, V)).GetEnumerator
        Throw New NotImplementedException()
    End Function
    Private Function IEnumerable_GetEnumerator() As IEnumerator Implements IEnumerable.GetEnumerator
        Throw New NotImplementedException()
    End Function
End Class

您只需要在每个操作下维护lookupitems的完整性。

最新更新