当实际为2时,Array.Length的计算结果为false?帮助组合+排序数组

时间:2014-02-13 19:27:49

标签: arrays vb.net sorting

在不使用Array.Sort函数的情况下,我应该从两个文件中读取数字(按照从最小到最大的顺序单独排序),并将它们合并到最小到最大顺序的列表框中。我认为这是一个优雅的问题解决方案,但有一个我似乎无法消除的错误。

如果没有看到它就很难解释,所以这里是我的代码(目前没有错误处理程序):

Private Sub btnMerge_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMerge.Click
    'read the files entered in the text boxes
    Dim file1() As String = IO.File.ReadAllLines(txtFile1.Text)
    Dim file2() As String = IO.File.ReadAllLines(txtFile2.Text)
    'simplify removal of the smallest item of the array.  
    '(the last item because the values in each file are sorted)
    Array.Reverse(file1)
    Array.Reverse(file2)

    While file1.Length > 0 Or file2.Length > 0 'as long as there are numbers left in either array
        If file1.Length And file2.Length Then 'numbers in both arrays
            Debug.WriteLine(file1.Last & " vs " & file2.Last)
            If CInt(file1.Last) < CInt(file2.Last) Then
                lstOutput.Items.Add(file1.Last)
                ReDim Preserve file1(file1.Length - 2) 'remove last (smallest) number
            Else
                lstOutput.Items.Add(file2.Last)
                ReDim Preserve file2(file2.Length - 2) 'remove last (smallest) number
            End If
        ElseIf file1.Length Then 'file2 is empty.  comparison of last values will throw an error
            Debug.WriteLine(file1.Last & " vs " & "Nothing")
            lstOutput.Items.Add(file1.Last)
            ReDim Preserve file1(file1.Length - 2) 'remove last (smallest) number
        ElseIf file2.Length Then 'file1 is empty.  comparison of last values will throw an error
            Debug.WriteLine("Nothing" & " vs " & file2.Last)
            lstOutput.Items.Add(file2.Last)
            ReDim Preserve file2(file2.Length - 2) 'remove last (smallest) number
        End If
    End While
End Sub

File1.dat:

12
23
34
45
46
52

File2.DAT的:

4
5
10
20
25

lstOutput中的输出是:

4
5
10
12
23
34 <-- what??
20
25
45
46
52

调试日志中的输出是:

  

12 vs 4

     

12 vs 5

     

12 vs 10

     

12 vs 20

     

23 vs Nothing&lt; - what ??

     

34 vs Nothing&lt; - what ??

     

45 vs 20

     

45 vs 25

     

45 vs Nothing

     

46 vs Nothing

     

52 vs Nothing

正如您所看到的,该算法在第五次比较和第六次比较之后表现完美。但是为什么在20之前将34插入列表框?调试日志中间的那两个“vs Nothing”来自哪里? file2.Length评估为False - 连续两次 - 当它实际为2时(通过断点确认)!

注意:我完全清楚不鼓励使用ReDim Preserve。如果您有理由相信这是问题,请仅对其进行评论。

2 个答案:

答案 0 :(得分:2)

你的IF不正确,就像你写的那样,它正在做一个按位操作。你应该有。

If file1.Length > 0 And file2.Length > 0 Then 

就个人而言,我只会保留每个数组当前位置的索引而不是反向/重新编号。

答案 1 :(得分:0)

我可以将其归结为几乎一行:

Private Sub btnMerge_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMerge.Click
    For Each i As Integer In IO.File.ReadLines(txtFile1.Text).Concat(IO.File.ReadLines(txtFile2.Text)).OrderBy(Function(s) Convert.ToInt32(s)).Distinct()
        lstOutput.Items.Add(i)
    Next 
End Sub

但是,它更具可读性:

Private Sub btnMerge_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnMerge.Click
    Dim numbers = File.ReadLines(txtFile1.Text) _ 
         .Concat(IO.File.ReadLines(txtFile2.Text)) 
         .Select(Function(s) Convert.ToInt32(s))
         .OrderBy(Function(i) i) _
         .Distinct()

    For Each i As Integer In numbers
        lstOutput.Items.Add(i)
    Next 
End Sub