vb.net 循环字典(Of String, List(Of String))
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31141659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Looping through Dictionary(Of String, List(Of String))
提问by Danny
Similar question here, but no answer : vb.net dictionary of string,dictionary...after filling readout always empty
类似的问题在这里,但没有答案:vb.net 字符串字典,字典......填充读数后始终为空
I fill as List(Of String) with rows of text from a file and then add it to a Dictionary. The method I use to fill this Dictionary works as it should. I create:
我将文件中的文本行填充为 List(Of String),然后将其添加到字典中。我用来填充这个字典的方法应该可以正常工作。我创造:
Private dictDictionary As Dictionary(Of String, List(Of String))
loop through the text file and adding each row to a list then add that list to the dictionary with the file name as key like so:
循环遍历文本文件并将每一行添加到列表中,然后将该列表添加到字典中,文件名作为键,如下所示:
dictDictionary.Add(sFileName, sFileRows)
sFileRows is a List(Of String) containing a MAX of 1056 elements that I need to move around based on specific options. The problem I'm having is accessing this List(Of Strings) by the Key.
sFileRows 是一个 List(Of String) ,包含最多 1056 个元素,我需要根据特定选项移动这些元素。我遇到的问题是通过键访问这个 List(Of Strings)。
I've tried:
我试过了:
For Each kvp As KeyValuePair(Of String, List(Of String)) In dictDictionary
Dim sKey As String = kvp.Key
Dim tempRows As List(Of String) = New List(Of String)
tempRows = dictDictionary.item(sKey)
Next
No matter what I try when I assign the List(Of String) in the Dictionary to a new List it is always empty. But the original dictionary has the rows in the List(Of String) that I read from the text file.
无论我在将字典中的 List(Of String) 分配给新列表时尝试什么,它始终为空。但是原始字典中有我从文本文件中读取的 List(Of String) 中的行。
First Method that fills the dictionary:
填充字典的第一种方法:
Private Sub GetInfo()
Try
Dim sFileName As String = String.Empty
Dim sFileRows As List(Of String) = New List(Of String)
If IO.Directory.Exists("some directory")Then
Dim Files() As String = IO.Directory.GetFiles("directory and file type")
For Each File As String In Files
sFileName = Path.GetFileName(File)
Dim rdrRows As StreamReader = New StreamReader(File)
Dim sString As String
While rdrRows.Peek() >= 0
sString = rdrRows.ReadLine()
sFileRows.Add(sString)
End While
'Actually adding the info to the dictionary
dictDictionary.Add(sFileName, sFileRows)
rdrRows.Dispose()
sFileRows.Clear()
Next
End If
Catch ex As Exception
End Try
End Sub
Second Method to manipulate the order of elements in the List(Of String)
操作 List(Of String) 中元素顺序的第二种方法
Private Sub ChangeStructure()
For Each kvp As KeyValuePair(Of String, List(Of String)) In dictDictionary
Dim rows As List(Of String) = kvp.Value
For Each item As String In rows
MessageBox.Show(item.ToString)
Next
Next
End Sub
There is nothing in the List(Of String) now but there was when it was filled in GetInfo()
List(Of String) 现在什么都没有,但是当它被填充到 GetInfo()
回答by sloth
Should be as easy as
应该很简单
Dim data = New Dictionary(Of String, List(Of String)) From _
{
{"Foo", New List(Of String) From {"1", "2", "3"}},
{"Bar", New List(Of String) From {"4", "5", "6"}}
}
For Each kvp in data
Console.WriteLine(kvp.Key & " says:")
For Each str in kvp.Value
Console.WriteLine(str)
Next
Next
回答by Joel Coehoorn
For Each kvp As KeyValuePair(Of String, List(Of String)) In dictDictionary
Dim rows As List(Of String) = kvp.Value
If rows Is Nothing Then Continue
For Each item As String in rows
'...
Next item
Next kvp
回答by Idle_Mind
You're are using kvp.Key, why are you not using kvp.Value?
你在用kvp.Key,为什么不用kvp.Value?
For Each kvp As KeyValuePair(Of String, List(Of String)) In dictDictionary
Dim sKey As String = kvp.Key
Debug.Print("Filename: " & sKey)
Dim tempRows As List(Of String) = kvp.Value
Debug.Print("# of Lines: " & tempRows.Count)
Debug.Print("------------------------------")
For Each line As String In tempRows
Debug.Print(line)
Next
Debug.Print("------------------------------")
Debug.Print("")
Next
回答by Chris Dunaway
The problem is this line:
问题是这一行:
sFileRows.Clear()
List(Of String) is a reference type. After you fill the list and add it to the dictionary, you clear it, so it is empty when you try to access it later.
List(Of String) 是一种引用类型。填写列表并将其添加到字典后,您将其清除,以便稍后尝试访问时它是空的。
The solution is to create a new list each time in the loop. In other words, move this line:
解决方案是每次在循环中创建一个新列表。换句话说,移动这一行:
Dim sFileRows As List(Of String) = New List(Of String)
inside the For loop and get rid of the call to .Clear()
在 For 循环内并摆脱对 .Clear()

