简体   繁体   English

如何将数组项列表存储到一个数组中

[英]how to store a list of array items in to one array

Here's a subroutine which reads a text file that includes an employee's details. 这是一个子例程,该子例程读取包含员工详细信息的文本文件。 I've stored for each line from the file to temporary array and I wanted to make it so that per 5 lines/5 array items, it stores them into one array. 我已经存储了从文件到临时数组的每一行,我想将其存储为每5行/ 5个数组项将其存储到一个数组中。 How do I make it so that storing array of size eg. 我如何做到这一点,例如存储大小数组 0-4 (5 items) into an array of type string? 0-4(5个项目)变成字符串类型的数组?

Example: 例:

  1. temparray[0] = John Frank temparray [0] =约翰·弗兰克
  2. temparray[1] = ID204 temparray [1] = ID204
  3. temparray[2] = Permanent worker temparray [2] =固定工人
  4. temparray[3] = Manager temparray [3] =管理员
  5. temparray[4] = 0 (additional payments) temparray [4] = 0(额外付款)

into one array that stores all these 5 lists, so maybe like: 放入一个存储所有这5个列表的数组中,所以可能像这样:

for x = 0 to 4 step 1 x = 0至4步骤1

store_array(1) = temparray(x) store_array(1)= temparray(x)

next x 下一个x

but after doing this, I used console.writeline to show store_array(1) and it only displays 0. 但是这样做之后,我使用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

You would be better off creating an Employee class - For example :- 您最好创建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

Then instead of using arrays, create a list of employees like this 然后,不使用数组,而是创建这样的员工列表

Dim employees As New List(Of Employee)

To read the data you can write it still using io.file, but to be honest I would recommend having a look as streamreaders and the keyword Using 要读取数据,您仍然可以使用io.file进行写入,但是老实说,我建议您看一下流读取器和关键字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

Now instead of referring to an employee using an array, which in 6 months time you won't remember which element means what, you can just use more obvious property names like so .. 现在,不用使用一个数组来引用一个雇员,在6个月的时间里您将不记得哪个元素意味着什么,您可以使用更明显的属性名称,例如..

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM