Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / February 2005

Tip: Looking for answers? Try searching our database.

Elements from array1 in array2?

Thread view: 
Amy Garrison - 21 Feb 2005 00:06 GMT
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.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.