1

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));
 }
 }
}
CSchulz
11.1k11 gold badges63 silver badges118 bronze badges
asked May 15, 2010 at 18:35
3
  • I guess you meant the tag java instead of javascript, so I retagged it. Commented May 15, 2010 at 18:37
  • please remember to put your code in a code block Commented May 15, 2010 at 18:38
  • I am not sure if it is just an error in copying, but I believe that it should be Comparable not comparable Commented May 15, 2010 at 18:44

6 Answers 6

7

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) {
answered May 15, 2010 at 18:43
Sign up to request clarification or add additional context in comments.

3 Comments

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

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

answered May 15, 2010 at 19:26

Comments

1

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() );
 }
 }
}
answered May 16, 2010 at 16:00

Comments

0

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

answered May 15, 2010 at 18:43

Comments

0

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

answered May 15, 2010 at 18:52

3 Comments

ok how would i do it if im going to use Comparator with 2 arguments?
@1ace1 didn't duffymo answered your question?
0

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

answered May 15, 2010 at 18:42

1 Comment

ok how would i do it if im going to use Comparator with 2 arguments?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.