Android Question compare array?

DaOne

Member
Licensed User
Longtime User
I am attempting to compare one array to another and make sure they do not match. What is the proper way to do this? My example does not work.

B4X:
Dim Array1(10) As String
Dim Array2(10) As String

If Array1 <> Array2 Then
   Do something
End If
 

stevel05

Expert
Licensed User
Longtime User
Array1 = Array2 set's the arrays to look at the same array, therefore array1 = array2 will always be true. If you change array1, array2 will change as well.

You can use java.util.Arrays with JavaObject to do the comparison:

B4X:
Public Sub EqualsObjArr(A() As Object, A2() As Object) As Boolean
    Dim JO As JavaObject
    JO.InitializeStatic("java.util.Arrays")
    Return JO.RunMethod("equals",Array As Object(A, A2))
End Sub

Other types can of array be compared as well as object(), take a look at the documentation: http://developer.android.com/reference/java/util/Arrays.html
 
Upvote 0

DaOne

Member
Licensed User
Longtime User
Thanks guys, I just used a For loop to set a flag if any of the elements did not match.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User


I'm wrong or that function compares the object pointers and then is the same thing?
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
No the function compares the array contents. Probably similar to the way DaOne has implemented it, although I would guess it would be quicker on large arrays rather than testing each element in this way.

I know when I tested the java.util.Arrays copying, there was minimal improvement over doing it in a loop, I think the compiler must be clever enough to use the native libraries in these cases.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…