我是一个菜鸟,所以请记住这个问题。 这是一段非常简单的代码:
Sub main() { Dim m_Dictionary as new Dictionary(Of Integer, List(Of String)) Dim workingList as new List(Of String) Dim workingKey as Integer Dim keyStash as List(Of Integer) Dim workingDict as new Dictionary(Of Integer, String) For i=0 to 9 Do workingKey = RandomInteger() Loop While workingList.ContainsKey(workingKey) For n=0 to 4 workingList.Add(RandomString()) Next keyStash.Add(workingKey) workingDict.Add(workingKey, workingList) Next ' now I just want to play back the generators of random data For each Key As Integer in keyStash For each Entry as String in workingDict(Key).Value Line(Entry) Next Next而不是像人们预期的那样好好地播放所有内容,我留下了完全准确的字典键。 但是,每个列表实例中的字符串值对于每个键都是相同的。 这些值等于随机数据生成的最后一个循环中的值。 因此,它不是回放50个唯一条目,而是写出最后一个循环的9倍。 我看着里面 - 一切都很好看。 得到这个。 所有列表,集合,哈希表,所有迭代类型以及自定义类型都演示了此行为。 我找到了解决方案,但它没有解释任何问题。 请有人帮忙解释一下!
i am a noob, so please take this question with that in mind. Here is very simple piece of code:
Sub main() { Dim m_Dictionary as new Dictionary(Of Integer, List(Of String)) Dim workingList as new List(Of String) Dim workingKey as Integer Dim keyStash as List(Of Integer) Dim workingDict as new Dictionary(Of Integer, String) For i=0 to 9 Do workingKey = RandomInteger() Loop While workingList.ContainsKey(workingKey) For n=0 to 4 workingList.Add(RandomString()) Next keyStash.Add(workingKey) workingDict.Add(workingKey, workingList) Next ' now I just want to play back the generators of random data For each Key As Integer in keyStash For each Entry as String in workingDict(Key).Value Line(Entry) Next NextInstead of everything playing back nicely as one might expect, I am left with a fully accurate stash of keys for the dictionary. However, the values for strings inside each list instance are ALL THE SAME FOR EVERY KEY. Those values are equal to the values in the last loop of random data generation. So instead of playing back 50 uniques entries, it writes out 9 times the last loop. I looked inside - everything looks good. Get this. All lists, collections, hash-tables, all of iterated types and also custom types demonstrate this behavior. I found the solution, but it does not explain anything. Can anyone help explaining this, please!??
最满意答案
保留由RandomString生成的字符串的变量是在循环外创建的。 在该循环内部,您将连续的新字符串添加到同一实例,并将相同的列表实例添加到每个新的整数键。 在循环结束时,添加的每个整数键的值都指向列表的相同引用。 当然他们是完全相同的....
您的代码的第一个修复可能是
Dim m_Dictionary as new Dictionary(Of Integer, List(Of String)) Dim workingKey as Integer For i=0 to 9 ' Internal to the loop. so at each loop you get a new list ' to use for the specific key generated in the current loop Dim workingList as new List(Of String) Do workingKey = RandomInteger() Loop While m_Dictionary.ContainsKey(workingKey) For n=0 to 4 workingList.Add(RandomString()) Next m_Dictionary.Add(workingKey, workingList) Next For each k in m_Dictionary For each Entry in k.Value ' Line(Entry) Console.WriteLine("Key=" & k.Key & " Values = " & Entry) Next Next请记住使用Option Strict ON ,当前代码将字符串静静地视为数字,这不是一个好习惯。 Option Strict ON会强制您在处理不同类型的数据时三思而后行。
The variable that keeps the strings generated by RandomString is created outside the loop. Inside that loop you add continuosly new strings to the same instance and add the same list instance to every new integer key. At the end of the loop every integer key added has its value pointing to the same reference of the list. Of course they are identical....
A first fix to your code could be
Dim m_Dictionary as new Dictionary(Of Integer, List(Of String)) Dim workingKey as Integer For i=0 to 9 ' Internal to the loop. so at each loop you get a new list ' to use for the specific key generated in the current loop Dim workingList as new List(Of String) Do workingKey = RandomInteger() Loop While m_Dictionary.ContainsKey(workingKey) For n=0 to 4 workingList.Add(RandomString()) Next m_Dictionary.Add(workingKey, workingList) Next For each k in m_Dictionary For each Entry in k.Value ' Line(Entry) Console.WriteLine("Key=" & k.Key & " Values = " & Entry) Next NextPlease, remember to use Option Strict ON, the current code treats quietly strings as if they were numbers, and this is not a good practice. Option Strict ON will force you to think twice when you work with different type of data.
更多推荐
发布评论