aspose file tools*
The moose likes Beginning Java and the fly likes Check an array for equality Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Check an array for equality" Watch "Check an array for equality" New topic
Author

Check an array for equality

ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424
I have an array of int type, I want check that array to see if it has equal elements for example: {1,1,1,1,1} return true, otherwise false.
I have written this code, it works well but I want know if its good or there better algorithm.

Scott Johnson
Ranch Hand

Joined: Aug 24, 2005
Posts: 518
See java.util.Arrays. It has some methods you'll be interested in.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

"Talt,"

Welcome to JavaRanch!

Please revise your display name to meet the JavaRanch Naming Policy. To maintain the friendly atmosphere here at the ranch, we like folks to use real (or at least real-looking) names, with a first and a last name.

You can edit your display name here. Thank you for your prompt attention!

-Marc


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14269
    
  21

Your code works, but looks unnecessarily complicated. You can do it in a more simple way like this:

[ January 10, 2007: Message edited by: Jesper Young ]

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Originally posted by Scott Johnson:
See java.util.Arrays. It has some methods you'll be interested in.


It does have some good methods for working with arrays, but not one for testing if all elements are the same ... does it?


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Gary Edwards
Greenhorn

Joined: Jan 10, 2007
Posts: 6
Originally posted by Peter Chase:


It does have some good methods for working with arrays, but not one for testing if all elements are the same ... does it?

You can always use Array's equals method:


[ January 11, 2007: Message edited by: Gary Edwards ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
There appear to be two different ideas here about what is meant by "check an array for equality". Gary and (maybe) Scott are talking about checking if two different arrays are equal to each other, in the sense that the corresponding elements are equal. I.e. a[0] == b[0], a[1] == b[1], etc. But the original poster and others are talking about checking one array to see if all the elements are equal to each other. I.e. a[0] == a[1] == a[2] etc. These are completely different problems; let's not mix them up.


"I'm not back." - Bill Harding, Twister
Gary Edwards
Greenhorn

Joined: Jan 10, 2007
Posts: 6
Oops...I really ought to learn how to read things a little more closely!
ahmed yehia
Ranch Hand

Joined: Apr 22, 2006
Posts: 424
The orignal problem I have is to check a grid of elements
modeled as 2D array to see if 1 row or column has equal elements.
However checking 1D array seems different.
Anton Uwe
Ranch Hand

Joined: Jan 10, 2007
Posts: 122
You could speed up the code a bit:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Check an array for equality