【问题标题】:List of Dictionary to multidimensional array字典列表到多维数组
【发布时间】:2018-01-12 18:32:29
【问题描述】:

我正在尝试将文件和路径列表传递给仅接受 String(,) 的第三方方法。此选择可能因用户选择而异。

我认为下面将代表二维数组,包含文件的路径和名称。

myFiles As New List(Of Dictionary(Of String, String))()

但是当我必须将它传递给例如方法时

ProcessFiles(ByVal Attachments(,) As String)

 ProcessFiles(myFiles.ToArray())

我得到了错误

"'Dictionary(Of String, string)() 的值不能转换为 String(,) 因为数组类型有不同的 尺寸。

如何定义我的列表来代表数组?

数组期望的布局为

(0,0) --> "\\location\Of\File"
(0,1) --> "filename"
(1,0) --> "\\location\Of\File2"
(1,1) --> "filename2"

【问题讨论】:

  • 字典键进入Attachments(X, ),然后值进入Attachments( , Y)ToArray 在字典(它本身是 2 个集合)上不会这样做。
  • 那么什么集合会起作用呢? Attachments(,) 代表的是(0,0) 应该是路径(0,1) 的名字,(1,0) 路径和(1,1) 的名字。因为我不知道开始的文件数,所以我希望执行一个 toArray 列表可以解决它。
  • 由于您的字典在列表中,因此 ToArray 只会返回一个字典数组。可能有一种 linqy 方式,但您可以迭代列表并将每个 KVP 复制到目的地
  • 我突然重新考虑我的字典列表。我的意图是获得一个干净的集合以轻松转换为数组。 ToArray 可以用于 VB 中的二维数组吗?对不起,这一切都是新的,继承了一些旧的但对我来说是新的东西。
  • 我/我们完全没有上下文,也不知道数据是什么样的。这些字典中有多少对 - 它们都只是 1 个键/值对还是它们是实际的集合。没有To2DArray() 方法所以答案取决于数据

标签: .net vb.net dictionary multidimensional-array


【解决方案1】:

这是一个相对简单的解决方案(基本上是 Plutonix 建议的),假设每个字典只有 1 个值,并且字典键是路径,值是名称:

    ' temporary dictionary for loop iteration
    Dim currentDict As Dictionary(Of String, String)
    ' assuming each dictionary only has 1 path/name entry, set up 2D string array
    Dim myFileArray(,) As String = New String(myFiles.Count - 1, 1) {}

    ' assuming the dictionary key is the path and the dictionary value is the name, 
    ' iterate through myFiles and extract key/value into 2D array
    For i = 0 To myFiles.Count - 1
        currentDict = myFiles(i)
        myFileArray(i, 0) = currentDict.Keys(0)
        myFileArray(i, 1) = currentDict.Values(1)
    Next

    ProcessFiles(myFileArray)

我玩过一些 LINQ 查询,但 .ToArray 会产生锯齿状数组(这与多维数组不同),因此如果您绝对需要二维数组,那么走这条路可能是不可能/不切实际的。

【讨论】:

  • 是的,就是这样。一个小注释 myFileArray(i, 1) = currentDict.Values(1) 应该是 myFileArray(i, 1) = currentDict.Values(0)。我最终错误地处理了 myFiles(i)。
【解决方案2】:

接受的答案通常是一种处理方式。

但是,如果您有重复的目录(即同一目录中的两个文件),则不能将目录用作字典的键,因为键必须是唯一的。

List(Of ... KeyValuePairstructTuplecustom classexisting class ) 将避开碰撞。

List(Of KeyValuePair) 与 Dictionary 很接近,因为它们在枚举时会暴露 KeyValuePair,您可以简单地将 garthhh 的答案中的 Dictionary(Of String, String) 替换为 List(Of KeyValuePair(String, String)) 以避免冲突。

具体来说,我认为System.IO.FileInfo 可能是您的案例的一个类,因为您正在处理文件。所以,使用List(Of System.IO.FileInfo) ...

Dim myFileListFileInfo As New List(Of System.IO.FileInfo) From {
    New FileInfo(System.IO.Path.Combine("\\location\Of\File", "filename")),
    New FileInfo(System.IO.Path.Combine("\\location\Of\File2", "filename2")),
    New FileInfo(System.IO.Path.Combine("\\location\Of\File3", "filename3"))
} ' initialize this way...
' ... or add like this
myFileListFileInfo.Add(New FileInfo(System.IO.Path.Combine("\\location\Of\File4", "filename4")))
Dim myFileArray(myFileListFileInfo.Count - 1, 1) As String
For i = 0 To myFileListFileInfo.Count - 1
    myFileArray(i, 0) = myFileListFileInfo(i).DirectoryName
    myFileArray(i, 1) = myFileListFileInfo(i).Name
Next

或者如果你真的只想要一个数组(,),这可能有点过头了。正如我所说,您可以使用许多东西来代替类、元组等中的 FileInfo。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-09
    • 1970-01-01
    • 2012-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-17
    • 1970-01-01
    相关资源
    最近更新 更多