VB.NET自定义类数组列表

VB.NET自定义类数组列表,vb.net,Vb.net,我目前正在尝试在vb.net中重新创建一个enigma机器作为良好的实践。 我正在尝试为转子创建一个自定义类 转子应包含整数移位,该移位计算其经过的移位数量,因此,如果达到26,则其他转子移位 还有一个26行的列表或数组,每个行包含26个插头 转子配置示例: 18 16 0 11 23 4 9 19 8 1 17 13 2 24 22 6 15 21 12 7 14 20 5 10 25 3 19 13 9 0 2 17 12 24 4 14 15 8 7 5 21 18 1 6 25 23 2

我目前正在尝试在vb.net中重新创建一个enigma机器作为良好的实践。 我正在尝试为转子创建一个自定义类

转子应包含整数移位,该移位计算其经过的移位数量,因此,如果达到26,则其他转子移位

还有一个26行的列表或数组,每个行包含26个插头 转子配置示例:

18 16 0 11 23 4 9 19 8 1 17 13 2 24 22 6 15 21 12 7 14 20 5 10 25 3
19 13 9 0 2 17 12 24 4 14 15 8 7 5 21 18 1 6 25 23 20 3 11 22 10 16
18 13 23 5 6 16 2 25 11 1 20 17 4 10 14 0 19 3 8 15 22 9 12 21 7 24
25 10 17 21 3 12 16 4 19 20 6 9 8 13 7 18 1 15 14 2 23 22 11 5 24 0
18 23 7 12 17 24 25 5 20 8 0 15 14 21 1 3 9 13 22 19 2 4 10 6 11 16
7 23 0 24 3 25 10 2 15 19 16 14 17 9 11 13 5 20 8 1 21 6 18 12 22 4
11 12 7 14 24 0 19 2 10 8 22 17 4 15 5 13 3 16 21 6 9 25 18 1 20 23
11 5 14 7 12 6 15 25 20 23 18 21 9 19 17 8 4 1 22 2 16 10 0 13 3 24
7 19 10 25 15 13 12 14 4 8 21 17 0 22 11 24 18 2 9 1 20 5 3 16 23 6
19 23 1 21 0 15 10 2 7 4 22 12 14 9 17 20 11 8 18 13 5 6 16 3 25 24
19 13 14 12 6 23 18 7 9 17 22 8 15 1 24 5 10 20 16 11 2 21 0 3 25 4
4 1 5 3 10 13 24 25 21 18 16 6 20 11 17 19 7 14 0 23 9 15 2 12 22 8
19 18 22 10 17 6 20 13 2 12 14 15 24 1 11 9 8 3 5 4 7 25 0 16 21 23
16 13 14 1 8 11 9 25 24 3 2 4 0 5 22 18 19 7 17 15 23 20 6 10 12 21
2 15 20 0 16 17 1 24 8 23 14 21 3 4 11 19 9 5 6 22 7 12 18 25 10 13
22 10 17 9 4 20 24 2 13 25 8 21 23 0 19 7 11 5 15 1 16 3 14 12 6 18
5 14 0 10 21 9 3 25 13 1 4 18 8 17 22 2 20 24 15 19 23 16 7 11 12 6
15 1 13 2 19 7 9 16 11 10 17 14 4 25 6 24 0 23 22 21 8 20 5 12 18 3
12 8 20 6 11 3 10 23 21 14 13 7 0 22 19 4 16 2 5 15 9 17 1 18 24 25
4 8 25 5 1 12 10 22 9 24 14 19 2 0 6 20 17 3 23 15 13 11 21 16 18 7
11 21 5 6 16 19 13 20 23 10 18 14 3 8 15 24 0 25 4 22 12 7 1 17 2 9
5 24 19 23 17 18 0 11 2 20 14 1 25 22 9 6 15 7 10 13 8 12 16 3 4 21
1 11 8 13 3 22 14 19 4 6 5 15 24 9 21 23 7 0 2 12 25 10 16 17 20 18
18 3 19 16 10 24 23 1 22 17 20 9 7 14 2 21 5 8 0 11 13 4 15 25 12 6
5 11 7 0 8 2 13 17 23 16 9 10 20 12 19 3 25 24 18 21 14 22 15 6 4 1
10 2 3 22 16 6 15 19 1 20 18 24 13 5 8 23 21 7 14 17 9 11 25 12 4 0
目前,我已经尝试过这个方法,但似乎效果不太好:

Public Class Rotor

    Public Property lines As New List(Of Liner)()
    Public Property shift As Integer
    Public Structure Liner
        Public Property plug() As Integer()
    End Structure
End Class
和我的配置加载程序:

Public Sub ConfigRotor(ByVal RotorConf As Rotor, ByVal PathConf As String)
    Dim i As Integer = 0
    For Each line As String In File.ReadAllLines(PathConf)
        Dim y As Integer = 0
        For Each strr As String In line.Split(" ")
            RotorConf.lines(i).plug(y)
            y = y+1
        Next

        i = i + 1
        If i > 26 Then
            MsgBox("Configuration file not supported")
        End If
    Next
End Sub

你能告诉我一个更优化的方法来轻松处理这个问题吗?

好吧,因为没有人可以帮助我做一些事情,这确实很难看,但很有效。如果有人对未来的参考感兴趣,我声明了两种结构:

Public Structure rotori
    Dim lines() As liner
    Public shift As Integer
End Structure

Public Structure liner
    Public plugs() As Integer
End Structure
然后我在应用程序启动时使用了ReDim,如下所示:

Dim Rotor1 As New rotori
ReDim Rotor1.lines(25)
For b As Integer = 0 To 25
     ReDim Rotor1.lines(b).plugs(25)
Next
Public Class Rotor

    Public Property lines As New List(Of Liner)
    Public Property shift As Integer
    Public Structure Liner
        Public Property plug As Integer()
    End Structure
End Class
Public Sub ConfigRotor(ByVal RotorConf As Rotor, ByVal PathConf As String)
    Dim myArray() As Byte = File.ReadAllBytes(PathConf)

    If (myArray.Length <> 676) Then
        MsgBox("Configuration file not supported")
    Else
        RotorConf.lines.Clear()
        For pos As Integer = 0 To myArray.Length - 1 Step 26
            Dim line As New Rotor.Liner
            Dim newPlug(25) As Integer
            line.plug = newPlug
            Array.Copy(myArray, pos, line.plug, 0, 26)
            RotorConf.lines.Add(line)
        Next
    End If
End Sub

首先,声明类的正确方法如下:

Dim Rotor1 As New rotori
ReDim Rotor1.lines(25)
For b As Integer = 0 To 25
     ReDim Rotor1.lines(b).plugs(25)
Next
Public Class Rotor

    Public Property lines As New List(Of Liner)
    Public Property shift As Integer
    Public Structure Liner
        Public Property plug As Integer()
    End Structure
End Class
Public Sub ConfigRotor(ByVal RotorConf As Rotor, ByVal PathConf As String)
    Dim myArray() As Byte = File.ReadAllBytes(PathConf)

    If (myArray.Length <> 676) Then
        MsgBox("Configuration file not supported")
    Else
        RotorConf.lines.Clear()
        For pos As Integer = 0 To myArray.Length - 1 Step 26
            Dim line As New Rotor.Liner
            Dim newPlug(25) As Integer
            line.plug = newPlug
            Array.Copy(myArray, pos, line.plug, 0, 26)
            RotorConf.lines.Add(line)
        Next
    End If
End Sub
为了获得有效的代码,我建议您将文件类型从文本更改为二进制。读取和操作字节数组并将其转换为int数组比使用字符串数组更容易、更快

我将假设您的文件是二进制文件,并且您已经在其中存储了所需的确切字节数(26 x 26=676字节)。代码如下所示:

Dim Rotor1 As New rotori
ReDim Rotor1.lines(25)
For b As Integer = 0 To 25
     ReDim Rotor1.lines(b).plugs(25)
Next
Public Class Rotor

    Public Property lines As New List(Of Liner)
    Public Property shift As Integer
    Public Structure Liner
        Public Property plug As Integer()
    End Structure
End Class
Public Sub ConfigRotor(ByVal RotorConf As Rotor, ByVal PathConf As String)
    Dim myArray() As Byte = File.ReadAllBytes(PathConf)

    If (myArray.Length <> 676) Then
        MsgBox("Configuration file not supported")
    Else
        RotorConf.lines.Clear()
        For pos As Integer = 0 To myArray.Length - 1 Step 26
            Dim line As New Rotor.Liner
            Dim newPlug(25) As Integer
            line.plug = newPlug
            Array.Copy(myArray, pos, line.plug, 0, 26)
            RotorConf.lines.Add(line)
        Next
    End If
End Sub
Public子配置转子(ByVal-RotorConf作为转子,ByVal-PathConf作为字符串)
Dim myArray()的字节=File.ReadAllBytes(PathConf)
如果(myArray.Length 676),则
MsgBox(“不支持配置文件”)
其他的
RotorConf.lines.Clear()
对于pos,整数=0到myArray.Length-1步骤26
将线路尺寸标注为新转子。衬板
将新插头(25)调整为整数
line.plug=newPlug
Array.Copy(myArray,pos,line.plug,0,26)
RotorConf.lines.Add(行)
下一个
如果结束
端接头
简单、快速、干净。
运行此代码的总时间少于1ms(取决于您的ard驱动器的速度)。

这可能更适合CodeReview?“y=+1”?这会一直把1放在y里面。我也不知道如何初始化数组。。。说“似乎不太管用”不是很清楚。什么不起作用?@荷花抱歉的打字错误哈哈纠正了it@Crimson-你怎么会打错字?这是否意味着你不只是复制/粘贴你的代码?@我所做的只是在复制粘贴后添加了那一行