Special

Clearance Sale!

We've been publishing for over five years now and it's time to clear out our inventory of back issues, so we're slashing prices!

RBD Magazines

Check out this amazing clearance sale of all our past issues. Missing some issues? This is a great time to complete your RBD collection. Save up to 40% off the regular price of our printed back issue packages. These prices are only good until the end of the year May 2008 and supplies are limited, so place your order today.

Article Preview


Buy Now

Print:
PDF:

Algorithms

Comparing Arrays

Do two arrays share data?

Issue: 5.1 (September/October 2006)
Author: Charles Yeomans
Article Description: No description available.
Article Length (in bytes): 8,921
Starting Page Number: 40
RBD Number: 5117
Resource File(s): None
Related Web Link(s):

http://www.declareSub.com

Known Limitations: None

Excerpt of article text...

to compare the contents of two arrays. While this is elementary, it is not entirely trivial.

The simplest problem is to determine whether two arrays contain the same objects in the same order. More precisely, we want to determine whether two arrays list1 and list2 have the same size, and whether list1(i) equals list2(i) for every index i from 0 to UBound. The simplest implementation expresses these criteria directly.

Function Equals(list1() as Object, list2() as Object) as Boolean

If UBound(list1) <> UBound(list2) then

Return false

End if

dim areEqual as Boolean = true

dim N as Integer = UBound(list1)

For i as Integer = 0 to N

areEqual = areEqual and (list1(i) is list2(i))

Next

Return areEqual

End Function

There is an obvious optimization. Since two arrays are equal only if list1(i) equals list2(i) for every value of i, we can abandon further comparison if we encounter a value of i for which list1(i) does not equal list2(i).

Function Equals(list1() as Object, list2() as Object) as Boolean

...End of Excerpt. Please purchase the magazine to read the full article.

Article copyrighted by REALbasic Developer magazine. All rights reserved.


 


|

 


Weblog Commenting and Trackback by HaloScan.com