By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,699 Members | 1,333 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,699 IT Pros & Developers. It's quick & easy.

Fastest way to compare arrays

P: n/a
What is the fastest way to determine if two arrays that
contain ValueTypes are equal? For example, lets say I
have the following:

Dim pt1 as New Drawing.Point(1, 2)
Dim pt2 as New Drawing.Point(2, 3)
Dim pt3 as New Drawing.Point(3, 4)
Dim pt4 as New Drawing.Point(4, 5)

Dim A() as Drawing.Point = {pt1, pt2, pt3}
Dim B() as Drawing.Point = {pt1, pt2, pt3}
Dim C() as Drawing.Point = {pt2, pt3, pt4}
Dim D() as Drawing.Point = {pt3, pt2, pt1}

What is the fastest way to determine that B equals A but
that C and D do not equal A? The only way I know is to
loop through the arrays and test each member (e.g.,
Drawing.Point.op_Equality(A(i), B(i)), but it seems like
there must be a better way.

Thanks for any help!
Nov 20 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
assuming { 1, 2, 3 } does not equal { 1, 3, 2 } (i.e. order matters in the
equality)
the only way to confirm to arrays are equal is to check every member...
however, you can note that two arrays are not equal as soon as two adjacent
members are not equal.

something like:

Function AreArraysEqual(ByRef one As Object(), ByRef two As Object()) As
Boolean

' check lengths are the same
If one.Length <> two.Length Then
Return False
End If

' check each element
For index As Integer = 0 To one.Length - 1
If (one.GetValue(index) <> two.GetValue(index)) Then
Return False
End If
Next

Return True
End Function

"Lance" <zi***@hotmail.com> wrote in message
news:00****************************@phx.gbl...
What is the fastest way to determine if two arrays that
contain ValueTypes are equal? For example, lets say I
have the following:

Dim pt1 as New Drawing.Point(1, 2)
Dim pt2 as New Drawing.Point(2, 3)
Dim pt3 as New Drawing.Point(3, 4)
Dim pt4 as New Drawing.Point(4, 5)

Dim A() as Drawing.Point = {pt1, pt2, pt3}
Dim B() as Drawing.Point = {pt1, pt2, pt3}
Dim C() as Drawing.Point = {pt2, pt3, pt4}
Dim D() as Drawing.Point = {pt3, pt2, pt1}

What is the fastest way to determine that B equals A but
that C and D do not equal A? The only way I know is to
loop through the arrays and test each member (e.g.,
Drawing.Point.op_Equality(A(i), B(i)), but it seems like
there must be a better way.

Thanks for any help!
Nov 20 '05 #2

P: n/a
"Lance" <zi***@hotmail.com> schrieb
What is the fastest way to determine if two arrays that
contain ValueTypes are equal? For example, lets say I
have the following:

Dim pt1 as New Drawing.Point(1, 2)
Dim pt2 as New Drawing.Point(2, 3)
Dim pt3 as New Drawing.Point(3, 4)
Dim pt4 as New Drawing.Point(4, 5)

Dim A() as Drawing.Point = {pt1, pt2, pt3}
Dim B() as Drawing.Point = {pt1, pt2, pt3}
Dim C() as Drawing.Point = {pt2, pt3, pt4}
Dim D() as Drawing.Point = {pt3, pt2, pt1}

What is the fastest way to determine that B equals A but
that C and D do not equal A? The only way I know is to
loop through the arrays and test each member (e.g.,
Drawing.Point.op_Equality(A(i), B(i)), but it seems like
there must be a better way.
That's also the only way I know.
Thanks for any help!

--
Armin

Nov 20 '05 #3

P: n/a
Thanks to both of you for the feedback.
Nov 20 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.