Java:整数 obj 无法转换为 Comparable

标签 java generic-programming comparable

我在尝试从驱动程序类传递 Integer 对象作为我创建的 SortedArray Generic 类的函数的参数时遇到问题。在我的驱动程序类中,我将用户的 int 输入转换为一个 Integer 对象,以转换为我的 SortedArray 类的 Comparable。

我继续收到错误:“线程“main”java.lang.ClassCastException 中的异常:java.lang.Integer 无法转换为 Comparable”。我查看了一些同学的源代码,发现在设置参数/参数方面几乎没有什么不同,但他们的代码工作得很好。我花了好几个小时试图找出我犯了什么错误,但我仍然找不到为什么我的 Integer 对象不能转换为 Comparable。

这是我的 SortedArray 类的一些内容

public class SortedArray implements Comparable{
    public int size;
public int increment;
public int top;
    Comparable[] a = new Comparable [size];

public SortedArray(int initialSize, int incrementAmount)
{
        top = -1;
        size = initialSize;
        increment = incrementAmount;

}
public int appropriatePosition(Comparable value)
{
        int hold = 0;
        if(top == -1)
        {
            return 0;
        }
        else
        {    
            for(int i = 0; i <= top; i++)
            {
                if(a[i].compareTo(value) > 0)
                {
                    hold = i;
                    break;
                }
            }
        }
        return hold;
}

public void insert(Comparable value) //The function that my driver class needs to access
{
        //Shifting numbers to the top
        if(full() == true)
        {
            Comparable[] tempArray = new Comparable[top + increment];
            for(int i= 0; i< size; i++)
            {
                tempArray[i]= a[i];
                a  = tempArray;
            }
            size = top + increment;
        }
        if(a[appropriatePosition(value) + 1] != null)
        {
            for(int i = top; i < appropriatePosition(value); i--)
            {
                a[i + 1] = a[i];
            }
        }
        a[appropriatePosition(value) + 1]= value;
    }

这是我的驱动程序类的代码,它传递 Integer Object insertObj 作为 SortedArray 的插入函数的参数。

public class IntDriver  {
 public static void main(String[] args)
 {
     Scanner keyboard = new Scanner(System.in);
     //Creating variables
     int data;
     boolean check = false;
     int choice;
     int size = 5;
     int increment = 3;
     SortedArray b = new SortedArray(size, increment);
     //Creating Menu
     while(check == false)
     {
     System.out.println("Please choose through options 1-6.");
     System.out.println("1. Insert\n2. Delete\n3. Clear\n4. Smallest\n5. Largest\n6. Exit");
     choice = keyboard.nextInt();
     switch(choice)
         {
         case 1:
             System.out.println("Type the int data to store in array location.");
             data = keyboard.nextInt();
             Integer insertObj = new Integer(data);
             b.insert(insertObj);// Here's where I lay "insertObj" as an argument for the SortedArray function.
             System.out.println("The value " + data + " is inserted");
            break;

最佳答案

问题是 Integer 扩展了 java.lang.Comparable,那么你的 Comparable 就不是 java.lang.Comparable 。看报错信息,你的Comparable来自默认包而不是java.lang:

Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to Comparable

也就是说,您不能将java.lang.Integer 转换为您自己的类

关于Java:整数 obj 无法转换为 Comparable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2112343/

相关文章:

c++ - 模板元编程——g++ 吃掉了它,clang 没有

c++ - 非模板错误的模板定义

JAVA GENERICS ERROR : have the same erasure, 但两者都没有覆盖另一个

java - 如何删除从Excel导入的字符串中的空格

Java - SQL时间戳的时区转换问题

java - 将 int 添加到 short

c++ - 为什么 list::unique 只删除 C++ 中的连续元素?

java - 如何排序和保留 web 服务的结果?

java - 如何让枚举在实现接口(interface)时使用它们的 compareTo 方法?

java - JPA实体和DTO属于Service层还是Spring Repository层