This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Soft Skills and have John Sonmez on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Finding an element in an array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Finding an element in an array" Watch "Finding an element in an array" New topic
Author

Finding an element in an array

Mirvs No
Greenhorn

Joined: Feb 21, 2011
Posts: 1
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

Joined: Jul 21, 2011
Posts: 177
    
    6

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.


Everything is theoretically impossible, until it is done. ~Robert A. Heinlein
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: Aug 20, 2011
Posts: 5
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
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

Joined: Aug 20, 2011
Posts: 5
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

Joined: Oct 13, 2005
Posts: 40052
    
  28
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;
 
wood burning stoves
 
subject: Finding an element in an array