1

i have an array

Dim A1 as array = { 4, 2, 7 }

i have an second array

Dim A2 as array = {{1, 21, 13}, {4 , 2, 7}, {5, 4, 5}} 

how can i find A1 in A2 with Array.Find() method?

Thanks

4
  • 1
    This is your answer msdn.microsoft.com/en-us/library/d9hy2xwa%28v=vs.110%29.aspx Commented May 16, 2015 at 22:45
  • thanks. i know but i couldnt with this method.. Commented May 16, 2015 at 22:52
  • 1
    Are you intending to use System.Array or do you actually want Dim A1() As Integer = {4, 2, 7} etc.? Array.Find specifies that the array to search is one-dimensional. Commented May 17, 2015 at 1:49
  • No as integer, as array. Array in array both are one dimensional. You can think as A1 = {"a", "b", "c"}, A2 = {{"d" , "f", "g"}, {"a", "b", "c"}, {"g", "h", "k"}} Commented May 17, 2015 at 7:24

2 Answers 2

1

Here is the closet thing I could think of that does what you're wanting with the Find() method.

If you change your 2D array to a List(Of Integer()) you can do the following:

Imports System
Imports System.Collections.Generic
Imports System.Linq

Public Module Module1

    Public Sub Main()
        Dim A1() as Integer = {4, 2, 7}
        Dim A2 As New List(Of Integer())
        A2.Add(New Integer() {1, 21, 13})
        A2.Add(New Integer() {4, 2, 7})
        A2.Add(New Integer() {5, 4, 5})

        Dim A3 = A2.Find(Function(A) A.SequenceEqual(A1))
        If Not A3 Is Nothing Then
            Console.WriteLine(String.Format("Array {0} found", String.Join(",", A1)))
        Else
            Console.WriteLine(String.Format("Array {0} not found", String.Join(",", A1)))
        End If
    End Sub
End Module

See running example here: https://dotnetfiddle.net/lDxQlW

Results:

Array 4,2,7 found

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

Comments

0

As a System.Array contains Objects, it is messy:

Option Strict On
Option Infer Off

Module Module1

    Function ArrayEquals(a As Array, b As Array) As Boolean
        If a Is Nothing AndAlso b Is Nothing Then
            Return True
        End If

        If a Is Nothing OrElse b Is Nothing Then
            Return False
        End If

        If a.Length <> b.Length Then
            Return False
        End If

        For i As Integer = 0 To a.GetUpperBound(0)
            If Not a.GetValue(i).Equals(b.GetValue(i)) Then
                Return False
            End If
        Next

        Return True

    End Function

    Sub Main()
        'Dim a As Array = {"a", "b", "c"}
        'Dim b As Array = {"d", "e", "f"}
        'Dim c As Array = {"g", "h", "i"}
        Dim a As Array = {1, 2, 3}
        Dim b As Array = {2, 3, 1}
        Dim c As Array = {3, 1, 2}

        Dim x As Array = {a, b, c}

        'Dim toFind As Array = {"d", "e", "f"}
        Dim toFind As Array = {2, 3, 1}

        Dim p As Object = Array.Find(CType(x, Object()), Function(z) ArrayEquals(CType(z, Array), toFind))

        If p Is Nothing Then
            Console.WriteLine("Not found.")
        Else
            ' Visualize the result:
            Select Case p.GetType().ToString()
                Case Is = "System.Int32[]"
                    Dim q As Int32() = DirectCast(p, Int32())
                    Console.WriteLine(String.Join(", ", q))
                Case Is = "System.String[]"
                    Dim q As String() = DirectCast(p, String())
                    Console.WriteLine(String.Join(", ", q))
            End Select

        End If

        Console.ReadLine()

    End Sub

End Module

Swap the commented and un-commented equivalents to show that it works with both.

I'm not saying it will work in a general case.

Comments

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.