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










