views:

112

answers:

6

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));
  }



 }

}
A: 

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

Robert
ok how would i do it if im going to use Comparator with 2 arguments?
+6  A: 

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) {
duffymo
yes you are correct i found my mistake thank you very much and im sorry for the miss tag ^^:
i miss spelled it... the only problem is it didnt say why in a clear way.
@1ace1 you should hit the check mark on this answer if it's the one that solved your problem.
drhorrible
A: 

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

crazyscot
A: 

[...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

OscarRyz
ok how would i do it if im going to use Comparator with 2 arguments?
@1ace1 didn't duffymo answered your question?
OscarRyz
@1ace1 I added a new answer: http://stackoverflow.com/questions/2841284/implementing-interface/2844404#2844404
OscarRyz
+1  A: 

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

Fisher
A: 

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() );
        }
    }
}
OscarRyz