Java:Integer obj无法强制转换为Comparable

TLM*_*TLM 3 java generic-programming comparable

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

我继续收到错误:"线程中的异常"主"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;
    }
Run Code Online (Sandbox Code Playgroud)

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

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;
Run Code Online (Sandbox Code Playgroud)

axt*_*avt 6

问题是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
Run Code Online (Sandbox Code Playgroud)

也就是说,你不能投java.lang.Integer你自己的班级


Pau*_*and 5

正如axtavt所提到的,问题是你有自己的类可比较.更具体地说,这意味着:

Integer.valueOf(1) instanceof java.util.Comparable == true
Integer.valueOf(1) instanceof Comparable == false
Run Code Online (Sandbox Code Playgroud)

这意味着您的代码中的某个位置具有以下内容:

Object[] a = new Object[] {Integer.valueOf(1);};
Comparable x = (Comparable) a[0];
// or something equivalent, this is likely being passed through layers
// and not being done next to each other like this.
Run Code Online (Sandbox Code Playgroud)

您需要将其更改为:

Object[] a = new Object[] {Integer.valueOf(1);};
java.util.Comparable x = (java.util.Comparable) a[0];
Run Code Online (Sandbox Code Playgroud)

更好的是,您应该将Comparator类重命名为不与Java中的标准类冲突的东西.通常,尽管Java具有命名空间,但您应该尽量避免使用与系统类同名的类来避免这种混淆.