关于集合乱序的问题
目前遇到一个问题,如何在知道集合元素Index的情况下,获取对应元素的key
Public Function 乱序(ByRef 集合 As Microsoft.VisualBasic.Collection) As Microsoft.VisualBasic.Collection Dim i, t, LBnd, UBnd As Integer Dim 临时键值 As String Dim 临时元素 As Object Dim 返回集合 As Microsoft.VisualBasic.Collection LBnd = 1 : UBnd = 集合.Count : Randomize() For i = LBnd To UBnd t = Int((UBnd - LBnd + 1) * Rnd() + LBnd) 临时元素 = 集合(t) 临时键值 = 集合(t).GetHashCode '如何获取元素键值??? If i = 1 Then MsgBox(临时键值) 集合.Remove(t) 集合.Add(临时元素, 临时键值) UBnd = UBnd - 1 Next 返回集合 = 集合 Return 返回集合 End Function
Public Function 乱序(ByRef 集合 As Microsoft.VisualBasic.Collection) As Microsoft.VisualBasic.Collection Dim list As New List(Of Object)() For Each item As Object In 集合 list.Add(item) Next Dim rndlist = list.Select(Function(x) Return New KeyValuePair(Of String, Of Object)(Guid.NewGuid().ToString(), x)).OrderBy(Function(x) Return x.Key).ToList() Dim 返回集合 As Microsoft.VisualBasic.Collection rndlist.ForEach(Function(x) 返回集合.Add(x)) Return 返回集合End Function