Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9082965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:40:27+00:00 2026-06-16T20:40:27+00:00

import java.util.*; class A implements Comparable<A> { int a; public int compareTo(A other) {

  • 0
    import java.util.*;  
    class A implements Comparable<A>  
    {  
        int a;  
        public int compareTo(A other)  
        {  
            A a1 = (A) other; //No need of cast  
            if(this.a == other.a ) return 0;  
            if(this.a < other.a )  return -1;  
            return 1;  
        }  
    }  
    class ctest  
    {  
        public static void main(String args[])  
        {    
            String[] names = {"OOP","BITS","PILANI"};  
            Arrays.sort(names);  
            int[] data = { 10,-45,87,0,20,21 };  
            Arrays.sort(data);    

            A[] arr = new A[5];  
            arr[0] = new A();  
            arr[1] = new A();  
            arr[2] = new A();  
            arr[3] = new A();   
            arr[4] = new A();  
            Arrays.sort(arr);  
        }  
    } 

This gives the error:

Exception in thread "main" java.lang.ClassCastException: A cannot be cast to java.lang.Comparable  
    at java.util.ComparableTimSort.countRunAndMakeAscending(Unknown Source)  
    at java.util.ComparableTimSort.sort(Unknown Source)  
    at java.util.ComparableTimSort.sort(Unknown Source)  
    at java.util.Arrays.sort(Unknown Source)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T20:40:28+00:00Added an answer on June 16, 2026 at 8:40 pm

    Exception in thread “main” java.lang.ClassCastException: A cannot be cast to java.lang.Comparable

    This means the version of A you are running is not Comparable. Most likely you have two versions of A or you didn’t build the class correctly. I suggest using an IDE to build you classes and doing a clean build should fix this.

    BTW: All your A are the same, so sorting them won’t do anything. You can fix these once you have fixed the build.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider this code: import java.util.*; class jm45 implements Comparator<jm45> { private int x; jm45(int
import java.util.HashMap; import java.io.*; import java.util.*; public class Fegan implements Comparable{ HashMap<String, Integer> cart
Here's Pair.java import java.lang.*; import java.util.*; public class Pair<TYPEA, TYPEB> implements Comparable< Pair<TYPEA, TYPEB>
import java.util.*; public class Employee { private int empId; private String name; private int
import java.util.concurrent.CountDownLatch; import java.util.concurrent.atomic.AtomicInteger; public class Main implements Runnable { private final CountDownLatch cdl1
This is my code: package test; import java.util.logging.Level; import java.util.logging.Logger; class Data{ int ar[]=new
import java.util.List; import java.util.ArrayList; interface Canine {} class Dog implements Canine {} public class
package data_structures; import java.util.StringTokenizer; public class ExpressionEvaluator implements Stack, Queue { private String userInput;
Here is the whole code : import java.util.ArrayList; public class Test<E extends Comparable<E>>{ ThreadLocal<ArrayList<E>>arraylist=new
I have realized some logic using integers import java.util.concurrent.locks.*; public class MassageSalon implements Salon{

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.