0

I have to check to see if the "tokens" in the arraylist are in increasing order.

private E[] data;      // where the data is stored   
private int size;      // how many items have been added to the list

public TopSpinArray(int numTokens, int spinSize)
{
    super(numTokens, spinSize);

    data = (E[])(new Object[numTokens]);
    size = 0;
} 

public boolean isSolved()
{     
    for(int i = 0; i < numTokens; i++)
    {
        if(data[i] < data[i+1])
        { 
            return true;
        }
    }
    return false;
}

when I compile, it says "bad operand types for binary operator '<' first type: E; second type: E"

how do I check to see if they are increasing?

3
  • 1
    Please add the declaration of 'data' - we need to know what type it is if we are to help you. Commented Apr 14, 2013 at 20:31
  • your loop will exit once first element turns out to be less than second. You need to return false once you see elements the other way around. wrt your compilation problem see atk's comment. Commented Apr 14, 2013 at 20:33
  • This is an array, not an ArrayList. Commented Apr 14, 2013 at 20:45

1 Answer 1

4

The operator < is only valid for certain types of objects. To be able to compare any comparable type of object (I take your E to imply that your tokens indeed implement Comparable, since otherwise your question makes no sense,) you can use compareTo():

for(int i = 0; i < numTokens - 1; i++) {
     if(data[i].compareTo(data[i+1]) > 0) { 
         return false;
     }
}
return true;
Sign up to request clarification or add additional context in comments.

1 Comment

@awashburn: They should be in ascending order, so if compareTo() returns something > 0, something is wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.