Vb.net 在Visual Basic中从文本文件向多维数组添加项

Vb.net 在Visual Basic中从文本文件向多维数组添加项,vb.net,sorting,multidimensional-array,streamreader,Vb.net,Sorting,Multidimensional Array,Streamreader,我有这个文本文件: Paul George|2.87|29 Stephen Curry|2.85|28 Jamal Murray|2.72|21 PJ Tucker|2.72|11 Kyle Lowry|2.61|15 Game Paul George|g2d|g2p Stephen Curry|g2d|g2p Jamal Murray|g2d|g2p PJ Tucker|g2d|g2p Kyle Lowry|g2d|g2p Game Paul George|g3d|g3p Stephen Cu

我有这个文本文件:

Paul George|2.87|29
Stephen Curry|2.85|28
Jamal Murray|2.72|21
PJ Tucker|2.72|11
Kyle Lowry|2.61|15
Game
Paul George|g2d|g2p
Stephen Curry|g2d|g2p
Jamal Murray|g2d|g2p
PJ Tucker|g2d|g2p
Kyle Lowry|g2d|g2p
Game
Paul George|g3d|g3p
Stephen Curry|g3d|g3p
Jamal Murray|g3d|g3p
PJ Tucker|g3d|g3p
Kyle Lowry|g3d|g3p
Game
Paul George|g4d|g4p
Stephen Curry|g4d|g4p
Jamal Murray|g4d|g4p
PJ Tucker|g4d|g4p
Kyle Lowry|g4d|g4p
我想将这些项添加到数组中

Names(name, gamenumber)
Distance(distance, gamenumber)
Points(Points, gamenumber)
第一个索引是玩家的数据,第二个索引是数据来自的游戏

比如说,

distance(1, 0) = 2.87
distance(5, 0) = 2.61
distance(1, 1) = g2d
以便索引与给定游戏编号的玩家匹配

到目前为止,我已经:

Private Sub openFile_Click(sender As Object, e As EventArgs) Handles openFile.Click
    OpenFileDialog.ShowDialog()
    Dim strFileName = OpenFileDialog.FileName
    Dim objReader As New System.IO.StreamReader(strFileName)
    Dim textline As String


    Dim Names(100, 3) As String
    Dim Distance(100, 3) As String
    Dim Points(100, 3) As String

    Dim Count As Integer = 0
    Dim GameNumber As Integer = 0


    Do While objReader.Peek() <> -1

        textline = objReader.ReadLine() & vbNewLine

        If textline = "Game" Then
            GameNumber = GameNumber + 1
        Else

            Dim parts() As String = textline.Split("|")


            Names(Count, GameNumber) = parts(0)
            Distance(Count, GameNumber) = parts(1)
            Points(Count, GameNumber) = parts(2)

            Count = Count + 1

        End If
    Loop

End Sub

如果您创建一个表示您的播放器的类并使用字典对它们进行索引,那么这将变得容易得多

Class Player
  Public Property Distances as List(Of Decimal)
  Public Property Points as List(Of Integer)
  Public Property Name as String

  Public Sub New(n as String)
    Name = n
    Distances = New List(of Decimal)
    Points = New List(of Integer)
  End sub 
End class
然后在读取文件的方法中:

Dim d as new Dictionary(of String, Person)
ForEach line as String in File.ReadAllLines(...)
  Dim bits = line.Split("|"c)
  If bits.Length < 3 Then Continue For

  If Not d.ContainsKey Then d.Add(bits(0), New Person(bits(0))

  Dim p = d(bits(0)) 'retrieve the old or just added person

  p.Distances.Add(decimal.parse(bits(1)))
  p.Points.Add(integer.parse(bits(2)))
Next line
或类似

要打印出它们的所有距离:

foreach s as string in d.Keys
  foreach dis as decimal in d(s).Distances
    Console.Write(d(s).Name & " got distance " & dis)
这是面向对象编程;我们使用类来表示事物和做有用的事情来模拟世界。我们不会试图在20个不同的数组中收集数据,并将其与索引等绑定在一起——这是一种非常程序化的代码思维,与vb.net的发明目的相反

实际上,这很可能不是一个合适的解决方案,也有点虚伪,因为我使用两个列表来跟踪距离和点,并断言列表索引是相等的-索引3处的距离和索引3处的点来自游戏4(零基索引注释)

我们真正应该做的是定义一个GameResult类,它具有距离、点数和gamenumber等属性,然后每个person类都有一个(GameResult)列表-person可以有一个函数,为此人返回格式良好的记分卡-这是正确的OO:)

Class Player
  Public Property Distances as List(Of Decimal)
  Public Property Points as List(Of Integer)
  Public Property Name as String

  Public Sub New(n as String)
    Name = n
    Distances = New List(of Decimal)
    Points = New List(of Integer)
  End sub 
End class
Dim d as new Dictionary(of String, Person)
ForEach line as String in File.ReadAllLines(...)
  Dim bits = line.Split("|"c)
  If bits.Length < 3 Then Continue For

  If Not d.ContainsKey Then d.Add(bits(0), New Person(bits(0))

  Dim p = d(bits(0)) 'retrieve the old or just added person

  p.Distances.Add(decimal.parse(bits(1)))
  p.Points.Add(integer.parse(bits(2)))
Next line
foreach s as string in d.Keys
  Console.Write(d(s).Name & " got avg distance " & d(s).GetAverageDist())
foreach s as string in d.Keys
  foreach dis as decimal in d(s).Distances
    Console.Write(d(s).Name & " got distance " & dis)