[英]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 ):
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.