Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Check an array for equality

 
ahmed yehia
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See java.util.Arrays. It has some methods you'll be interested in.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15279
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Gary Edwards
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Gary Edwards
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops...I really ought to learn how to read things a little more closely!
 
ahmed yehia
Ranch Hand
Posts: 424
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 122
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could speed up the code a bit:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic