【问题标题】:how to store a list of array items in to one array如何将数组项列表存储到一个数组中
【发布时间】:2018-07-21 03:27:50
【问题描述】:

这是一个读取包含员工详细信息的文本文件的子程序。 我已将文件中的每一行存储到临时数组中,并且我想将其存储为每 5 行/5 个数组项,它将它们存储到一个数组中。我如何做到这一点,以便存储大小的数组,例如。 0-4(5项)成字符串类型的数组?

例子:

  1. temparray[0] = 约翰·弗兰克
  2. temparray[1] = ID204
  3. temparray[2] = 永久工人
  4. temparray[3] = 经理
  5. temparray[4] = 0(额外付款)

到一个存储所有这 5 个列表的数组中,所以可能像:

对于 x = 0 到 4 步骤 1

store_array(1) = temparray(x)

下一个 x

但是在这样做之后,我使用了 console.writeline 来显示 store_array(1),它只显示 0。

    Dim arremp() As String = IO.File.ReadAllLines(filename)     'adds each line as string array
    Dim temp As String = " "
    Dim flen As Integer = arremp.Length - 1 'get string length, index based, starting at 0 not 1.
    Dim temparray(flen) As String   'string array
    Dim i As Integer = 0
    '   Setting up an object variable for the streamreader (which is the path folder and its name).
    Dim objreader As New System.IO.StreamReader(filename, True) ' Has been set to true because the file exists and this will append(ADD) for the next str.
    '   if a file doesn't exists, it should be false which will ensure that a text file is created first.

    For Each myline In arremp
        temp = myline & vbCrLf
        temparray(i) = temp
        i = i + 1
    Next myline

【问题讨论】:

  • 这是 VB.NET 代码 - 不是 VBA。
  • @QHarr 我的错,这是在 VB.NET 代码中,JohnyL 已经说过了。我在问题下方添加了示例。
  • 我有点困惑,但你可能想看看 System.Collections 下的 ArrayLists docs.microsoft.com/en-gb/dotnet/api/…
  • 好吧,干杯@QHarr
  • 还有多维数组之类的东西。在你的情况下是二维的。但我认为 Davids 关于类(或结构)的想法要好得多。

标签: .net arrays vb.net visual-studio visual-studio-2012


【解决方案1】:

您最好创建一个 Employee 类 - 例如:-

Private Class Employee
    Public Property Name As String
    Public Property ID As String
    Public Property Type As String
    Public Property JobTitle As String
    Public Property AdditionalPayments As Integer
End Class

那么不要使用数组,而是创建一个这样的员工列表

Dim employees As New List(Of Employee)

要读取数据,您仍然可以使用 io.file 来写入数据,但老实说,我建议您查看 streamreaders 和关键字 Using

Private Sub EmployeeData()
    'this is a more readable way of creating a nonexistant file or
    'reading from an existing one
    If Not IO.File.Exists(filename) Then
        'if the file exists create it
        IO.File.Create(filename)
        tempfile.Close()
    Else
        'otherwise read the data from the existing file
        Dim arremp() As String = IO.File.ReadAllLines(filename)     'adds each line as string array
        'you dont need i to track the number of items in the list of employees, just use employees.Count

        'Loop through the array 5 items at a time, create a tempEmployee
        'and add it to the list of employees
        For x As Integer = 0 To arremp.Count - 1 Step 5
            Dim tempEmployee As New Employee With {.Name = arremp(x),
                                                   .ID = arremp(x + 1),
                                                   .Type = arremp(x + 2),
                                                   .JobTitle = arremp(x + 3),
                                                   .AdditionalPayments = arremp(x + 4)
                                                   }
            employees.Add(tempEmployee)
        Next
    End If
End Sub

现在,与其使用数组来引用员工,那样在 6 个月内您将不记得哪个元素意味着什么,您可以使用更明显的属性名称,例如 ..

TextBox1.Text = employees(i).Name
TextBox1.Text = employees(i).JobTitle

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-28
    • 1970-01-01
    • 2020-02-04
    • 2016-06-28
    • 2017-11-16
    • 1970-01-01
    • 2021-01-19
    相关资源
    最近更新 更多