I have need for the following.
Given any two arrays of ints
array1 = [1, 5, 7]
array2 = [1, 2, 7, 9]
I need to determine if both arrays contain the same values...
though not necessarily in the same order.
So, the above would result in 'false'.
Comparing
array1 = [1, 3, 5]
array2 = [1, 5, 3]
would result in true.
array1 = [1]
array2 = [1, 3]
would result in false... etc.
I have the two arrays, but is there any way to compare
the values without looping through the second array for
each value in the first?
Thanks.
Takuya Murata - 21 Feb 2005 00:32 GMT
I guess you can do:
Collection matches = new HashSet (Arrays.asList (array1)).retainAll
(Arrays.asList (array2)).
if (matches.size () > 0)
; // there is a match.
else
; // there is none.
although I am not sure why simple looping poses any problem.