user319570
user319570

Reputation: 45

implementing interface

hello guys so i have this assignment that i need to implement interface to go over an ArrayList and sort it (ascending or descnding).I dont want "the" answer i just need some suggestions on my approach and why i get this error

Exception in thread "main" java.lang.ClassCastException: Week7.Check cannot be cast to java.lang.Comparable
 at java.util.Arrays.mergeSort(Unknown Source)
 at java.util.Arrays.sort(Unknown Source)
 at java.util.Collections.sort(Unknown Source)
 at Week7.TestCheck.main(TestCheck.java:18)

This is how i did it:

comparable had one method called public int compairTo(Object o):

public class Check implements comparable {
    private Integer checkNumber;

    public Check(Integer newCheckNumber) {
        setCheckNumber(newCheckNumber);
    }

    public String toString() {
        return getCheckNumber().toString();
    }

    public void setCheckNumber(Integer checkNumber) {
        this.checkNumber = checkNumber;
    }

    public Integer getCheckNumber() {
        return checkNumber;
    }

    @Override
    public int compairTo(Object o) {
        Check compair = (Check) o;
        int result = 0;
        if (this.getCheckNumber() > compair.getCheckNumber())
            result = 1;

        else if (this.getCheckNumber() < compair.getCheckNumber())
            result = -1;

        return result;
    }
}

in my main i had this

import java.util.ArrayList;
import java.util.Collections;

public class TestCheck {
    public static void main(String[] args) {
        ArrayList checkList = new ArrayList();

        checkList.add(new Check(445));
        checkList.add(new Check(101));
        checkList.add(new Check(110));
        checkList.add(new Check(553));
        checkList.add(new Check(123));

        Collections.sort(checkList);

        for (int i = 0; i < checkList.size(); i++) {
            System.out.println(checkList.get(i));
        }
    }
}

Upvotes: 1

Views: 1001

Answers (6)

OscarRyz
OscarRyz

Reputation: 199215

To use Comparator ( what you call with two arguments ) you have to pass it as a parameter to the Collections.sort method.

Like this:

Collections.sort( checkList, new Comparator<Check>() {
    public int compare( Check one, Check two ) {
        return one.getCheckNumber() - two.getCheckNumber();
    }
 });

This is how it would look like:

import java.util.List;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Collections;

public class Check   {
    private final Integer checkNumber;

    public Check(Integer newCheckNumber) {
        this.checkNumber = newCheckNumber;
    }
    public Integer getCheckNumber() {
        return this.checkNumber;
    }
    public static void main( String [] args ) {
        List<Check> list = new ArrayList<Check>();

        list.add(new Check(445));
        list.add(new Check(101));
        list.add(new Check(110));
        list.add(new Check(553));
        list.add(new Check(123));

        Collections.sort( list, new Comparator<Check>() {
           public int compare( Check one, Check two ){
               return one.getCheckNumber() - two.getCheckNumber();
           } 
        });
        for( Check item : list ) {
            System.out.println( item.getCheckNumber() );
        }
    }
}

Upvotes: 1

Fisher
Fisher

Reputation: 248

I guess the interface comparable you implements is not the interface java.lang.Comparable, their name might be the same, but the package?

Upvotes: 2

Robert
Robert

Reputation: 8609

when you implement an interface you must use the method signature provided by the interface, exactly as it is defined in the interface:

public int compareTo(Object o) 

Also you might like to parameterize the List eg. List<Comparable>, and the Comparable eg. Comparable<Check> as using generics is a compile time technique which would have identified your misspellings

Upvotes: 0

OscarRyz
OscarRyz

Reputation: 199215

[...And] why i get this error

When you call:

Collections.sort(checkList);

In line 18, the method is expecting that your elements implement Comparable ( to be able to compare them ) You might not know this, but that's how it works ( as specified in the documentation )

All elements in the list must implement the Comparable interface

Then the method runs, it tries to "cast" your objects to the type Comparable since your class doesn't implements it, it throws ClassCastException

To better understand this, try the following code:

class Some {
}
class Test {
    public static void main( String [] args ) {
         Runnable r = ( Runnable ) new Some();
    }
}

If you analyze the line you may think "Why are you trying to assign a Runnable from Some? They are not related!"

And you're right, let's see the output:

$ java Test
Exception in thread "main" java.lang.ClassCastException: Some cannot be cast to java.lang.Runnable
        at Test.main(A.java:5)

Same error message. You may take a look at line 5 and see the cast.

That's exactly what it's happening in the Collections.sort method, but with Comparable

Upvotes: 0

crazyscot
crazyscot

Reputation: 11989

Whatever you pass to Arrays.sort() must implement Comparable, which is a java system interface. You probably want to throw away comparable.

Upvotes: 0

duffymo
duffymo

Reputation: 308743

C'mon - Java is case sensitive. "comparable" is not the same as "Comparable"

public class Check implements comparable

Spelling matters as well. "compairTo" isn't the same method as "compareTo"

@Override
public int compairTo(Object o) {

Upvotes: 7

Related Questions