Java:整数obj无法转换为Comparable [英] Java: Integer obj can't cast to Comparable
问题描述
在尝试从驱动程序类传递Integer对象作为我创建的SortedArray Generic类的函数的参数时遇到问题。从驱动程序类中,我将用户的int输入转换为要转换为我的SortedArray类的Comparable的Integer对象。
I'm having problems trying to pass an Integer object from a driver class as an argument for function of a SortedArray Generic class I created. From my driver class, I convert the user's int input into an Integer object to be cast onto Comparable of my SortedArray class.
我继续收到错误: Exception in线程主要 java.lang.ClassCastException:java.lang.Integer无法强制转换为Comparable。我看了看我同学的一些源代码,发现在设置参数/参数上没有什么区别,但他们的代码运行得很好。我一直在寻找试图找出自己犯了什么错误的小时,但仍然找不到为什么我的Integer对象无法转换为Comparable的原因。
I continue to receive the error: "Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to Comparable". I took a look at some of my classmates' source codes only to find little difference in setting the arguments/parameters yet they have their code working just fine. I've been looking for hours trying to find what error I've made and I still can't find why my Integer object can't be cast to Comparable.
这是我的SortedArray类别中的一些内容
Here's a bit from my SortedArray Class
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的插入函数。
Here's the code for my driver class that passes Integer Object insertObj as an argument for SortedArray's insert function.
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
:
The problem is that Integer
extends java.lang.Comparable
, then your Comparable
is not a java.lang.Comparable
. Look at the error message, your Comparable
comes from the default package rather than java.lang
:
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to Comparable
也就是说,您不能将 java.lang.Integer
转换为自己的类
That is, you can't cast java.lang.Integer
to your own class
这篇关于Java:整数obj无法转换为Comparable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!