Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

How can I tell if the elements in an Array List are same or different?

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm trying to verify if all the elements in an array list are same or not. This is my code:

 
Marshal
Posts: 79632
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
. . . and does your code work? What sort of output do you get? Are you suffering any exceptions?
Don't declare your List as ArrayList; declare it as List. Don't call a List arr; it isn't an array. Call it numbers or something like that.
Why have you got the bang sign/not operator in line 3? I think that shouldn't be there.
If you think about the different kinds of collection/data structure available, which you can read about here, you will find a collection type whose size() method will tell you how many distinct elements you have.
 
Ranch Hand
Posts: 218
5
MS IE Notepad Suse
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the questions is not unique defined - are you try to look for any matches or do you want all entries the same (i cant come up with a usefull case for the latter one)?

also: why get(z++).equals(get(z--))?
simple write: get(z).equals(get(z+1))

if you really want to all entries the same: for(Integer integer : list) ...
this way you make use of List.iterator() - depend on impl of list may faster
 
Marshal
Posts: 28288
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Matt Wong wrote:also: why get(z++).equals(get(z--))?
simple write: get(z).equals(get(z+1))



I'm glad you clarified that, Matt. If I was told to look at that code and see what it did, I would have had to get pencil and paper and spend a minute to figure out the first version. I definitely support writing simple code which other people can understand.
 
Paul Clapham
Marshal
Posts: 28288
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Samantha Rupasingha wrote:I'm trying to verify if all the elements in an array list are same or not.



Then take the first entry in the list and compare it to all of the other entries. As soon as you find one entry which isn't the same as the first entry, you know the answer is No. If you don't find any, then the answer is Yes.

Your code doesn't do that. Apparently it tries to compare adjacent entries; that would work the same way in that finding a pair of adjacent entries which are different means No, but you can't say Yes until you're finished and haven't said No. (Also you have an off-by-one error whereby you will go past the end of the list and an exception will be thrown.)
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Samantha Rupasingha wrote:I'm trying to verify if all the elements in an array list are same or not. This is my code:


I think everyone else has covered it, but the basic problem you have is that the code doesn't do what you meant it to.

And when that happens, sometimes you just have to sit down with a paper and pencil and work out what your loop will do on each iteration.

However (TIP) - DON'T use '++' or '--' more than once in a loop. And if you do, it should usually be inside the 'for' part.

You also need to read Paul's second post. Is that what you want to do? If so, he's given you the solution; if not, you need to explain what you DO want to do.

Winston
 
reply
    Bookmark Topic Watch Topic
  • New Topic