Java to VB.net Migration 2D Array声明

时间:2016-12-04 23:02:11

标签: java arrays vb.net

我一直在尝试迁移代码,并且有一个2d数组正在返回一个空引用异常:对象变量或With块变量未设置。现在转换器有关于它的以下注释所以我试图初始化一个常规的2d数组没有RectangularArray.ReturnRectangularStringArray但我没有任何成功。我有一种感觉这是许多空异常中的第一个。代码基本上包含两个列表,其中一个日志的LxWxH已经预先编入一个块。还有另一种可以制作的削减类型及其废料的价值和价值。该算法最大化切割以获得最大块的木块,总结了你得到的东西。

Imports System
Imports System.Collections.Generic
Imports Microsoft.VisualBasic
Imports System.Data.OleDb
Imports System.Data.SqlClient
Imports System.IO
Imports System.Text.RegularExpressions

Public Class LogMatrix
    Public matrix()() As String
    Dim lumber As List(Of Lumber)
    Public Property RectangularArrays As Object

Public Sub New(ByVal a As Integer, ByVal b As List(Of Lumber))
    'JAVA TO VB CONVERTER NOTE: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization that is automatic in Java:
    'ORIGINAL LINE: matrix = new String[a+1][a+1]
    matrix = RectangularArrays.ReturnRectangularStringArray(a + 1, a + 1)
    matrix(a + 1, a + 1)

    lumber = b
    buildMatrix()
    printMatrix()
End Sub

Public Overridable Sub buildMatrix()
    ' Populate our profit table with the values of each cut
    For i As Integer = 0 To lumber.Count - 1
        matrix(lumber(i).width)(lumber(i).height) = "" & lumber(i).value
        matrix(lumber(i).height)(lumber(i).width) = "" & lumber(i).value
        administrator.ListBox1.Items.Add(lumber(i).ToString())
    Next i

    For i As Integer = 1 To matrix.Length - 1
        Dim j As Integer = i
        Do While j < matrix(i).Length
            Dim bestCut As String = "-1"

            ' For a piece of lumber size i by j, try every cut
            For k As Integer = 0 To lumber.Count - 1
                ' Make sure this size cut is possible
                If i - lumber(k).height < 0 OrElse j - lumber(k).width < 0 Then
                    Continue For
                End If

                ' The profit we get from cutting a single board of size cut.size
                Dim cutProfit As Double = lumber(k).value

                ' We can split up the leftovers in one of two ways
                Dim leftover As String = "" & matrix(i - lumber(k).height)(j) & " " & matrix(lumber(k).height)(j - lumber(k).width)
                Dim leftovers As String = "" & matrix(i - lumber(k).height)(lumber(k).width) & " " & matrix(i)(j - lumber(k).width)

                ' Pick the greatest profit from the leftover boards
                Dim leftoverProfits As String
                If count(leftover) > count(leftovers) Then
                    leftoverProfits = "" & leftover
                Else
                    leftoverProfits = "" & leftovers
                End If

                ' Take the highest profit from all possible cuts
                If cutProfit + count(leftoverProfits) > count(bestCut) Then
                    bestCut = "" & cutProfit & " " & leftoverProfits
                End If
            Next k

            ' If no cuts are possible, we can only make 0 profit.
            If count(bestCut) > count(matrix(i)(j)) Then
                matrix(i)(j) = "" & bestCut
            End If
            matrix(j)(i) = matrix(i)(j)
            j += 1
        Loop
    Next i
End Sub

Public Overridable Function count(ByVal str As String) As Double
    If str Is Nothing Then
        Return -1.0
    End If

    Dim count_Renamed As Double = 0.0
    Dim parser() As String = str.Split(New Char() {" "c})
    'Dim parser() As String = str.Split(" ", True)
    For k As Integer = 0 To parser.Length - 1
        If Regex.IsMatch(parser(k), "[a-zA-Z ]*\d+.*") Then
            count_Renamed += Double.Parse(parser(k))
        End If
        'If parser(k).matches("[a-zA-Z ]*\d+.*") Then
        '    count_Renamed += Double.Parse(parser(k))
        'End If
        Next k
    Return count_Renamed
End Function

Public Overridable Sub printMatrix()
    administrator.ListBox1.Items.Add(ControlChars.Lf)
    For i As Integer = 1 To matrix.Length - 1
        For j As Integer = 1 To matrix.Length - 1
            Dim parser() As String = matrix(i)(j).Split(New Char() {" "c})
            ' Dim parser() As String = matrix(i)(j).Split(" ", True)
            For k As Integer = 0 To parser.Length - 1
                If Regex.IsMatch(parser(k), "[a-zA-Z ]*\d+.*") Then
                    Console.Write(Double.Parse(parser(k)) & " ")
                End If
                'If parser(k).matches("[a-zA-Z ]*\d+.*") Then
                '    Console.Write(Double.Parse(parser(k)) & " ")
                'End If
            Next k
            administrator.ListBox1.Items.Add("|" & ControlChars.Tab)
        Next j
        administrator.ListBox1.Items.Add(ControlChars.Lf)
    Next i
End Sub

Public Overridable Function calculate(ByVal log As Log) As Double
    Dim total As Double = 0.0
    Do While log.length > lumber(0).length
        Dim parser() As String = matrix(log.height)(log.width).Split(New Char() {" "c})
        'Dim parser() As String = matrix(log.height)(log.width).Split(" ", True)
        For i As Integer = 0 To parser.Length - 1
            If Regex.IsMatch(parser(i), "[a-zA-Z ]*\d+.*") Then
                ' If parser(i).matches("[a-zA-Z ]*\d+.*") Then
                Dim j As Integer = 0
                Do While j < lumber.Count
                    If Double.Parse(parser(i)) = lumber(j).value Then
                        lumber(j).quantity = lumber(j).quantity + 1
                        total += lumber(j).value
                        log.length = log.length - lumber(0).length
                    End If
                    j += 1
                Loop
            End If
        Next i
    Loop

    Dim leftover As Integer = log.width * log.height * log.length
    lumber(lumber.Count - 1).quantity = lumber(lumber.Count - 1).quantity + leftover
    total += lumber(lumber.Count - 1).value * leftover
    Return total
   End Function
End Class

下面是错误enter image description here

的图片

0 个答案:

没有答案