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

Finding an element in an array

 
Mirvs No
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

This is my first post but I've used Java Ranch many times in the past. It's great finding a strong forum with a user base full of knowledgeable people.

-Thanks


Here's what I'm trying. I'd like to fill an array list with a class, Person, and check whether or not a class is contained in the array list based on a field in Person.



a class "Person"


and the main:


This returns false. To my understanding if I overrode the compare method in my class the ArrayList.contains() method would use that to make the comparison. This strikes me as a problem that would be common and I'm having a difficult time finding a solution.

On a side note, I thought about using a HashMap and just using the key as a means of getting around my own limitations or writing a custom collection. I don't need to worry about run times or memory, I'm just trying to learn if there is a practical way of accomplishing something like this. Any help or a point in the right direction is muchos appreciated.
 
Tina Smith
Ranch Hand
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method you're looking for is equals( Object obj ). If you're thinking of using the object in a HashMap, you should probably override hashCode() as well.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48984
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

It is not normal for a value class to implement the Comparator interface. If you have a class which has a "natural ordering", then it should implement the Comparable<T> interface. It would only require very slight changes to what you wrote.
 
suryakanta sahoo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Sheriff
Pie
Posts: 48984
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Writing == true? Never do that. It is not only bad style, but also prone to errors if you write = by mistake. It's if (b) ... else ... and if you want it false, write if (!b) ...
Also poor design, not making the fields private. Use getters for access to the name.

Style: your fields should not start with Capital Letters: name not Name. Choose better names for your variables; t is by no means an informative name. Leave spaces between tokens, as in the code following, which also shows you how to run that loop without break:

You ought to be overriding the equals method (as you have already been told), not trying to work out its innards in a different class. It is the class's own responsibility to decide on equality, not leaving it to another class.
Beware: you will not find writing an equals() method at all easy. You can search for details, since there are lots of questions about it on the Ranch.
 
suryakanta sahoo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know it is bad style to design.The variable in the class should start with small letter and subsequent words should be begin with capital letter.
But problem given by Mirvs No,i try to solve it by taking same name for instance variables.
And I like your above solution and i learn from it.
Thanks
 
Campbell Ritchie
Sheriff
Pie
Posts: 48984
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is more usual to use an index variable in a search loop. You would write it like this. The -1 value has been used for "not found" since the days of C (about 1972) and maybe even earlier.That is similar to indexOf methods. Note you only find the first instance; you need a different starting position from 0 if you want to find a second instance. You can convert it to a contains() method by adding a return statement like this: return index >= 0;
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic