代码之家  ›  专栏  ›  技术社区  ›  madlan

数组基础知识-使用循环填充

  •  0
  • madlan  · 技术社区  · 14 年前

    这是正确的方法吗?

    Dim ZipNameArray(?)
    
    Using zip As ZipFile = ZipFile.Read(ZipToUnpack)
        For Each file In zip
            ZipNameArray(?) = file .FileName
        Next
    End Using
    

    如何增加数组?文件不是数字吗(它是一个ZipEntry)

    4 回复  |  直到 14 年前
        1
  •  3
  •   Tim Schmelter    14 年前

    我会用一个 generic List(of ZipFile) 为了这个。它们更安全,更容易阅读。

    Dim zips as new List(of ZipFile)
    
    Using zip As ZipFile = ZipFile.Read(ZipToUnpack)
            For Each file In zip
               zips.add(file)
            Next
    End Using
    

    当你想迭代的时候:

    For each zip as ZipFile in zips 
         dim fileName as string=zip.FileName
    Next
    

    在99%的情况下,你可以忘记.Net中的数组,当你需要一个数组时,你就可以得到它 List.ToArray

        2
  •  1
  •   Andrew Lewis    14 年前

        3
  •  0
  •   Kyra    14 年前

    因为您不知道数组大小,所以有两种选择。你可以把Zip文件翻两遍。第一次只需计算文件数,然后创建数组,然后通过第二次添加每个文件的名称。

    如果zip文件太大,您可以将数组初始化为某个常量(比如10),当达到第11个文件名时,您可以通过“重排”数组来增加数组

    例如:

    Dim Names(10) as String
    Dim counter as Integer
    counter = 0
    Go through zip {
       counter += 1
       if counter = size of Names then
           ReDim Preserve Names(size of Names + 10) 
       add fileName
     }
    

    有关阵列(包括redim)的详细信息,请参阅 here .

        4
  •  0
  •   Christian Hayter    14 年前
    Dim zipNameArray As String()
    Using zip As ZipFile = ZipFile.Read(ZipToUnpack)
        zipNameArray = zip.Select(Function(file) file.FileName).ToArray()
    End Using