简体   繁体   English

在 Java 中对数组列表进行排序时无法转换为类错误

[英]Cannot be cast to class error when sorting an arraylist in Java

I'm trying to add a number to an object array list and I keep getting an 'Cannot be cast' error.我正在尝试将一个数字添加到对象数组列表中,但我不断收到“无法转换”错误。

I'm fairly new to Java so please go easy on me我对 Java 还很陌生,所以请放轻松

My code is below, any help would be appreciated.我的代码如下,任何帮助将不胜感激。 If anyone has any questions please contact me如果有人有任何问题,请与我联系

public class runningClass {

    public class Sortbymake implements Comparator<runningClass> 
    { 
        @Override
        public int compare(runningClass a, runningClass b) 
        { 
            return b.numb.compareTo(a.numb);
        }
    }

    private String numb;

    public runningClass(String nNumb)
    {
        this.numb = nNumb;
    }

    public String getNumber(){
        return numb;
    }

    public void oof()
    {
        Scanner scan = new Scanner(System.in);
        while(ptr <= 5)
        {
            System.out.println("What number do you want to add");
            System.out.println("Ptr = "+ptr);
            this.numb = scan.next();
            numberList.add(new runningClass(this.numb));
            ptr++;
        }
        Collections.sort(numberList, null);
    }

    public void searchList()
    {
        int Sptr = 0;
        Scanner scan = new Scanner(System.in);
        System.out.println("What is the number your searching for?");
        String userSearch = scan.next();
        for(int u = 0; u < numberList.size();u++)
        {
            //loop is created, will check if any index contains the entered text
            if(numberList.get(Sptr).getNumber().contains(userSearch))
            {
                System.out.println("The "+userSearch+" is located at index "+u);
                break;
            }
            else
            {
                Sptr++;
            }
        }
    }

}

And here's the error I'm getting, please contact me if you want me to post the full error这是我得到的错误,如果你想让我发布完整的错误,请联系我

Exception in thread "main" java.lang.ClassCastException: class arraysearch.runningClass cannot be cast to class java.lang.Comparable (arraysearch.runningClass is in unnamed module of loader 'app'; java.lang.Comparable is in module java.base of loader 'bootstrap')

Your class RunningClass is not implementing Comparable and Collections.sort is expecting a Comparable since you are not specifying any comparator instance.您的类RunningClass没有实现 Comparable 并且Collections.sort期待 Comparable 因为您没有指定任何比较器实例。

Why do you define a new class Sortbymake that implementes Comparator and never use it anywhere?为什么要定义一个实现 Comparator 的新类Sortbymake并且从不使用它?

I think you don't understand the difference between Comparator and Comparable (from source ):我认为您不了解 Comparator 和 Comparable 之间的区别(来自source ):

  • Comparable : Comparable is an interface defining a strategy of comparing an object with other objects of the same type. Comparable :Comparable 是一个接口,定义了将一个对象与其他相同类型的对象进行比较的策略。 This is called the class's “natural ordering”;这称为类的“自然排序”;
  • Comparator : The Comparator interface defines a compare(arg1, arg2) method with two arguments which represent compared objects and works similarly to the Comparable.compareTo() method. Comparator :Comparator 接口定义了一个带有两个参数的 compare(arg1, arg2) 方法,这些参数代表比较对象,其工作方式与 Comparable.compareTo() 方法类似。

Now, you have two solutions (and the first one feels the right one to me):现在,您有两个解决方案(第一个对我来说是正确的):

Use the Comparable interface in this case on your RunningClass and call the Collections.sort(numberList) method.在这种情况下,在您的RunningClass上使用 Comparable 接口并调用Collections.sort(numberList)方法。 This will make use of the implementation of the compareTo method enclosed in the RunningClass class.这将利用包含在RunningClass类中的compareTo方法的实现。

Use the Sortbymake class but specify the comparator when calling the sort method: Collections.sort(numberList, new Sortbymake())使用Sortbymake类,但在调用排序方法时指定比较器: Collections.sort(numberList, new Sortbymake())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 接收 Java 错误 - ArrayList$SubList 无法转换为 class - Receiving Java Error - ArrayList$SubList cannot be cast to class java.util.Vector 无法转换为 java.util.ArrayList 在 java 中解析 json 时出错 - java.util.Vector cannot be cast to java.util.ArrayList error when json is parsed in java ArrayList无法强制转换为自定义类扩展ArrayList - ArrayList cannot be cast to custom class extending ArrayList ArrayList的迭代器错误:无法将java.util.LinkedHashMap强制转换为MyObject - Iterator error for ArrayList: java.util.LinkedHashMap cannot be cast to MyObject Rest Assured execption 类 io.restassured.path.xml.XmlPath 无法转换为类 java.util.ArrayList 错误 - Rest Assured execption class io.restassured.path.xml.XmlPath cannot be cast to class java.util.ArrayList error Hibernate java.util.ArrayList无法强制转换..类强制转换异常 - Hibernate java.util.ArrayList cannot be cast.. Class cast exception 类 java.util.ArrayList 不能转换为类 org.springframework.util.MultiValueMap - class java.util.ArrayList cannot be cast to class org.springframework.util.MultiValueMap java.util.ArrayList无法转换为 - java.util.ArrayList cannot be cast to Java 1.6 arraylist对象无法转换为字符串 - java 1.6 arraylist object cannot cast to string 无法转换为java.util.ArrayList - Cannot be cast to java.util.ArrayList
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM