0

I am having a hard time to get this to work. The isLoop() method gets called every time something is added to the array to make sure there are no duplicates. When there is a duplicate, it should return true. But it isn't.

isLoop(){
    if(!Arrays.asList(SQUARES).contains(sq)){
        return false;
    }else{
        return true;
    }
}

Am I missing something fundamental about arrays? I am fairly new to Java, so it is probably the case! :D

EDIT

I have been asked to show how the objects were instansiated, so I have posted the whole class here:

class Happy {
static int SQUARES[] = new int[10000];
static int sq;

public static boolean isHappy(int num){
    int s = 0;
    int n = num;

    int i=0;

    for(sq = num; (!isLoop()) && (sq > 1);){
        sq = addSquaresOfDigits(sq);
        SQUARES[i] = sq;
        i++;

    }

    if(isLoop()){
        return false;
    }else{
        return true;
    }
}

static int addSquaresOfDigits(int number) {
    int result = 0;
    int tmp;
    while(number > 0) {
        tmp = number % 10;
        result += tmp * tmp;
        number /= 10;
    }
return result;
}

static boolean isLoop(){
    if(!Arrays.asList(SQUARES).contains(sq)){
        return false;
    }else{
        return true;
    }
}

The method isHappy() will test the number given it to see whether it is a happy number or not.

Isaac Lewis
  • 1,189
  • 2
  • 14
  • 26

5 Answers5

0

Make sure you define equals() and hashCode() on the sq class. See this question for details on that.

If the sq instance doesn't implement equals() then it will use the default implementation in Object which is to check if the two objects are the same instance.

Community
  • 1
  • 1
Valdis R
  • 2,095
  • 1
  • 18
  • 36
  • sq must be an instance of a class, or the snippet you've added wouldn't compile. Can you update the question to show how you've instantiated `sq`? – Valdis R Feb 16 '11 at 01:00
0

For a start you can simplify your code to the following:

isLoop(){
    return Arrays.asList(SQUARES).contains(sq);
}

Despite what you've said elsewhere, SQUARES must be an array and sq must be an object instance of a Class or your code wouldn't compile.

The equals() method of sq will have to return true when called on one of the objects in SQUARES for isLoop() to return true.

Alb
  • 3,601
  • 6
  • 33
  • 38
0

Consider switching to some Set implementation (e.g. HashSet class) to search if collections does or does not contain particular element. It's much faster then looping over the list. A

As mentioned aboce, remember about equals() and hashcode() methods.

Krzysztof Wolny
  • 10,576
  • 4
  • 34
  • 46
0

You state in a comment you haven't overriden equals() or hashCode(), this will be your issue as by default (and by design) Java doesn't know if two objects are meaningfully equal (since this will differ depending on the class!)

Have a look here for a good guide on the equals() and hashCode() methods and how to implement them properly. Alternatively, if you're feeling lazy, many IDEs will implement them for you based on a number of chosen fields. I'd only recommend this approach when you know how they work however!

As an aside, it seems you're using a list here when a HashSet would be a much better choice - by design it doesn't contain duplicates and is very fast at detecting them (it doesn't need to go through the entire list to check them as you're doing here.)

Michael Berry
  • 70,193
  • 21
  • 157
  • 216
  • I just edited my original question to include the full class that the isLoop() method sits in. All other begginner java mistakes aside, how would I use a Hash Set? Like this? `Set SQUARES = new HashSet();` – Isaac Lewis Feb 16 '11 at 01:19
0

Why are you converting SQUARES to list , i think that to use contains method ,you are using this conversion.

For example consider ,if you want to get the index of an value i.e. static int sq = 9;

Arrays.binarySearch(SQUARES,sq);  // returns the index at which the value 9 is present
Dead Programmer
  • 12,427
  • 23
  • 80
  • 112