0

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.

6
  • 1
    Where do I compare 'ag' to 'New Integer() {1, 2}'? Commented Jul 31, 2017 at 15:45
  • 2
    Don't use ArrayList Commented Jul 31, 2017 at 15:45
  • I compared the contents of 'ag' to 'New Integer() {1, 2}' Commented Jul 31, 2017 at 15:45
  • What else could I use? Commented Jul 31, 2017 at 15:46
  • you are not comparing/testing the contents of anything. New Integer() {1, 2} creates a new array Commented Jul 31, 2017 at 15:47

2 Answers 2

3

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()).

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I guess I'll just do the value comparison, hope it doesn't slow down the program too much.
Thanks a lot, but I can't do that in my program.
0

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

1 Comment

Thanks but, I'm not trying to find a integer in a list, but a list of integers in a list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.