Arraylist.Contains不返回真实的VB.NET

时间:2019-01-17 22:03:38

标签: vb.net arraylist

comps。即使comps包含,它也不会返回TRUE。

我已逐步调试它,但看不到问题出在哪里。

顺便说一句,代码的目的是显示总计为SUM值的对。 (如果总和为5,并且有1和4,则代码​​应返回1和4)

Public Function getPairs(ByVal array As ArrayList, ByVal sum As Integer)
    Dim comps, pairs As New ArrayList
    For index = 0 To array.Count - 1
        If (comps.Contains(array(index)) = True) Then
            pairs.Add(array(index))
        Else
            comps.Add(sum - array(index))
        End If
    Next
    Return pairs
End Function

Sub Main()
    ' 1,3,2,5,46,6,7,4
    ' k = 5
    'Dim arraylist As New ArrayList()
    Console.Write("Enter your array :")
    Dim arraylist As New ArrayList
    arraylist.AddRange(Console.ReadLine().Split(","))
    Console.Write("Enter the sum:")
    Dim sum As Integer = Console.ReadLine()
    getPairs(arraylist, sum)
    Console.ReadKey()
End Sub

3 个答案:

答案 0 :(得分:1)

您从用户输入中填充的ArrayList包含字符串(拆分用户输入字符串的结果)。 comps ArrayList包含整数(减法结果)。当它试图在包含2的ArrayList中找到字符串“ 2”时,它将失败。

您应该将用户输入转换为整数,以便比较相同的数据类型。

答案 1 :(得分:1)

首先,打开Option Strict。工具菜单->选项->项目和解决方案-> VB默认值。这将指出您的代码存在问题,并帮助您避免运行时错误。

ArrayList在新代码中很少使用,但是它是为了向后兼容。 List(Of T)是新代码的更好选择。

Module Module1
    Sub Main()
        ' 1,3,2,5,46,6,7,4
        ' k = 5
        'Dim arraylist As New ArrayList()
        Console.Write("Enter your array :")
        Dim arraylist As New ArrayList
        'Option Strict would not allow this line to compile
        '.Split takes a Char, the same c tells the compiler that "," is a Char
        arraylist.AddRange(Console.ReadLine().Split(","c))
        Console.Write("Enter the sum:")
        'Option Strict would not allow a string to be dumped into an integer
        Dim sum As Integer
        Dim Pairs As New ArrayList
        If Integer.TryParse(Console.ReadLine, sum) Then
            'Your Function returns an array list but you
            'throw it away by not setting up a variable to receive it
            Pairs = getPairs(arraylist, sum)
        Else
            Console.WriteLine("Program aborted. Sum was not a number.")
        End If
        For Each item In Pairs
            Console.WriteLine(item)
        Next
        Console.ReadKey()
    End Sub
    'Functions need a return data type in the declaration
    Public Function getPairs(ByVal array As ArrayList, ByVal sum As Integer) As ArrayList
        Dim comps, pairs As New ArrayList
        For index = 0 To array.Count - 1
            'I don't see how this can ever be true since comps is empty
            If comps.Contains(array(index)) Then 'Since .Contains returns a Boolean, no = True is necessary
                pairs.Add(array(index))
            Else
                'Ideally each item in array should be tested to see if it is a number
                'You will get an exception if CInt fails
                comps.Add(sum - CInt(array(index)))
                'You never use the comps ArrayList
            End If
        Next
        'The pairs ArrayList is empty
        Return pairs
    End Function
End Module

我看不出这段代码如何实现您所描述的目标。我认为您应该重新开始。讨论如何完成任务。然后将其写在纸上,而不是代码中。然后,您将更清楚地看到如何对项目进行编码。

答案 2 :(得分:0)

最大的问题是原始代码是这一行:

Dim comps, pairs As New ArrayList

该代码创建了两个ArrayList 参考变量,但是只创建了一个ArrayList object comps仍为null / Nothing

但是除此之外,{。{1}}类型自从.Net 2.0于2005年问世以来……已经有10多年了。它仅在今天才存在,以便与旧代码向后兼容。 请勿使用!

这是更好的做法,尤其是与ArrayListOption Strict结合使用:

Option Infer
相关问题