简体   繁体   中英

Find Integer array in ArrayList VB.NET

So I'm using ArrayLists to store some integer arrays and I came up with the following problem:

Public Class Form1

    Public ag As New ArrayList

    Sub a() Handles Me.Load

        ag.Add(New Integer() {1, 2})

        If ag.Contains(New Integer() {1, 2}) Then

            MsgBox("aaa")

        End If

    End Sub

End Class

The MsgBox won't show although the ArrayList does contain "New Integer() {1, 2}", and even if I try this:

Public Class Form1

    Public ag As New ArrayList

    Sub a() Handles Me.Load

        ag.Add(New Integer() {1, 2})
        Dim t = New Integer() {1, 2}

        For Each it In ag

            If it.Equals(t) Then

                MsgBox("aa")
                Exit For

            End If

        Next

    End Sub

End Class

It won't show at all.

Thanks in advance.

---------------EDIT---------------

I finally decided to just compare the values of the Integer lists like this:

Public Class Form1

    Public ag As New List(Of Integer())

    Sub a() Handles Me.Load

        ag.Add(New Integer() {1, 2})
        Dim t = New Integer() {1, 2}

        For Each it In ag

            If it(0) = t(0) And it(1) = t(1) Then

                MsgBox("aa")
                Exit For

            End If

        Next

    End Sub

End Class

Thank you all for your replies.

Arrays are reference types. Reference types use reference equality by default for both the .Equals() and = comparison. Since you're comparing against New Integer{1,2} in both cases (emphasis on the New ), you're comparing against two different references. Even though the two arrays have the same value, they are two different objects in memory, each with it's own different reference, and so the reference comparison will always return false.

In order to make this work the way you want, you need to do a value-comparison instead of a reference comparison. Unfortunately, there is no mechanism built into .Net to do value comparisons between arrays . You will have to implement your own EqualityComparer from scratch.

Unless.

You don't say where these arrays come from. If you are able to manage these arrays such that you can compare the array with the same reference, you can make this work:

Public Class Form1

    Public ag As New List(Of Integer())

    Sub a() Handles Me.Load
        Dim t As New Integer() {1,2}

        ag.Add(t)

        If ag.Contains(t) Then

            MsgBox("aaa")

        End If

    End Sub

End Class

And while I'm here, you should also change ArrayList to List(Of Integer()) .

I'm not sure if a List accepts the initialization you're doing (new {1,2)...), but anyway you can use LINQ to accelerate your search:

 dim ag as new ListOf(Integer)
 ag.Add(1)
 ag.Add(2)

 dim WHat2Find as integer = 2
 Dim Located As Integer = Ag.FindIndex(Function(y) y.Contains(What2Find))
 If Located > -1
       '  Found!
 End If

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM