Java:整型对象无法强制转换为可比较类型。

3
我在尝试将一个整数对象从驱动程序类作为参数传递给我创建的SortedArray泛型类的函数时遇到了问题。从我的驱动程序类中,我将用户的int输入转换为Integer对象,以便将其强制转换为我的SortedArray类的Comparable。
我继续收到错误消息:"Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to Comparable"。我查看了一些同学的源代码,发现在设置参数/参数方面几乎没有什么区别,但是他们的代码却可以正常工作。我已经找了几个小时,试图找出我的错误所在,但我仍然找不出为什么我的整数对象无法强制转换为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对象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;

2
你可以尝试在这里添加完整的异常堆栈跟踪。 - Michael Brewer-Davis
1
在您发布的代码中,没有任何内容被强制转换。您是否可以发布一个SSCCE,以展示您所描述的行为?http://sscce.org - Bart Kiers
3个回答

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

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

5

正如axtavt所提到的,问题在于您有自己的Comparable类。更具体地说,这意味着:

Integer.valueOf(1) instanceof java.util.Comparable == true
Integer.valueOf(1) instanceof Comparable == false

这意味着在你的代码中某处有类似以下的内容:

这意味着在你的代码中某处有类似以下的内容:

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.

你需要将其更改为:
Object[] a = new Object[] {Integer.valueOf(1);};
java.util.Comparable x = (java.util.Comparable) a[0];

更好的做法是将您的Comparator类重命名为不会与Java标准类冲突的名称。一般来说,尽管Java有命名空间,但您应该尽量避免您的类与系统类具有相同的名称,以避免出现这种混淆。


0
我并没有花太多精力在这个问题上,但是使用SortedSet实现(或其子接口NavigableSet),例如TreeSet比编写自己的类更容易。当然,除非你需要重复元素...

1
我看了一下我的一些同学的源代码,我想这个班级是他的作业。 - Brett Widmeier
好的,我已经给这个问题打上了作业标签。 - Powerlord

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接