1

I am trying to search through a collection of an ArrayList if pairs. What I want to be able to do, is to go through the collection and find the first value in a pair and return the second value of that pair. The problem I am having is that the check I have to find the first value doesn't seem to be working, so every time I search, I end up returning null. I know that the problem exists with my if statement, but I cannot seem to sort out what it is I am doing wrong. Since this is a homework assignment, I can't show all the code to my pair class, or my pair list class, but I can show you the method I have for searching the first value:

public S findFirst(F firstValue) {
    Iterator<Pair> myIter = this.iterator();
    S tmp2 = null;
    while (myIter.hasNext()) {
        Pair tmp1 = myIter.next();
        if (tmp1.getFirst() == firstCall) {
            tmp2 = (S) tmp1.getSecond();
        }
    }
    return tmp2;
}

If I throw in an else statement that just calls what I am attempting to do in my if check, like this:

else{
           tmp2 = (S) tmp1.getSecond(); 
        }

then whenever I test for the first value, I get the second value, so I know I am at least on the correct path, but I am assuming that I am doing something wrong with what I am checking for in my if statement. Does anyone know how I can correctly do this, (and please bear in mind that this is homework, so a guide to how to figure this out is far more valuable to me than just some random answer, I want to learn, not just be given an answer) Thanks in advance!

Xanthian23
  • 55
  • 9

4 Answers4

2

Don't use == to compare objects. Override and use equals().

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • You're correct. I didn't even think about that, I was too busy just trying to make the comparison, which is sad because I used the .equals in my last assignment and actually argued with someone over why its smarter to use that over the equals sign. I have made the change and now everything works perfect. Thank you so much. – Xanthian23 Nov 17 '13 at 20:57
  • Of course I have now realized a new problem, I can only search through the last entry in the collection, so if I add a pair into the collection of say, one and two, and then another pair of three and four, if I use this method to find one, I get null, the fun never ends... – Xanthian23 Nov 17 '13 at 22:53
2

I think

if (tmp1.getFirst() == firstCall)

should probably say

if (tmp1.getFirst().equals(firstValue))

The important difference is that == checks whether two expressions refer to the exact same object. You're more interested in knowing whether your two expressions actually refer to objects that are equal.

Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
1

Try this:

if (tmp1.getFirst().equals(firstValue))

instead of

if (tmp1.getFirst() == firstCall)

Also you can override your own equals method.

You should never use == to compare objects.

Check How to compare two java objects

Community
  • 1
  • 1
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
0

What Matt says, (don't use == ) but I think a bigger problem is that you don't return the 'first' encounter.... your if statement should look like:

public S findFirst(F firstValue) {
    Iterator<Pair> myIter = this.iterator();
    while (myIter.hasNext()) {
        Pair tmp1 = myIter.next();
        if (firstValue.equals(tmp1.getFirst())) {
            return (S) tmp1.getSecond();
        }
    }
    return null;
}
rolfl
  • 17,539
  • 7
  • 42
  • 76