File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Comparing two arrays Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Comparing two arrays" Watch "Comparing two arrays" New topic

Comparing two arrays

Jay Damon
Ranch Hand

Joined: Jul 31, 2001
Posts: 282
Is there a way to compare the contents of two arrays, e.g. that two String arrays contain the same element values in the same order?
pascal betz
Ranch Hand

Joined: Jun 19, 2001
Posts: 547
java.util.Arrays contains useful methods related to .... Arrays :-)
there are various methods to compare arrays (boolean arrays, int arrays, ...)

Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Since you care about order, some interesting approaches come up. If they are SORTED this works nicely:

For lists that are NOT sorted, I have an old Pascal program that looks ahead a certain distance (could be to the end on arrays) to decide which one has elements that are not in the other.

Late breaking thought: My Text Diff program compares arrays of strings, more or less.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Jay Damon
Ranch Hand

Joined: Jul 31, 2001
Posts: 282
Thanks for the replies. The equals method on the Arrays class was what I needed. I had looked at the Arrays class prior to posting this question but for some reason overlooked the equals method at that time.
I agree. Here's the link:
subject: Comparing two arrays
It's not a secret anymore!