簡體   English   中英

如何將數組項列表存儲到一個數組中

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

這是一個子例程,該子例程讀取包含員工詳細信息的文本文件。 我已經存儲了從文件到臨時數組的每一行,我想將其存儲為每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

您最好創建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進行寫入,但是老實說,我建議您看一下流讀取器和關鍵字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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM