我的Java应用程序中的Caugth ClassCastException [英] Caugth ClassCastException in my Java application
问题描述
使用数组的队列实现,但出现异常。
A queue implementaion using an array but i m getting an exception.
我有一个名为Queue的接口,泛型ArrayQueue作为队列的实现类Array InterfaceTest作为我测试代码的主要类。
I have an interface named as Queue with generic ArrayQueue as implemented class of Queue Interface ArrayQueueTest as my main class to test the code.
public interface Queue<E>
{
public void enqueue(E e);//insert an element in a queue
public E dequeue();/delete an element in queue and return that element
public int size();//give the number of elements in an queue
public E first();//give the first element of queue if any but not removing it
public boolean isEmpty();//indicate whether queue is empty or not
}
public class ArrayQueue<E> implements Queue<E>
{
E [] data; //array based implementation queue
int front; //indicating the first element of queue
int size; //size of queue indicator
ArrayQueue(int x) //initialization of queue
{
data=(E [])(new Object[x]);
}
public boolean isEmpty()
{
return size==0;
}
public int size()
{
return size;
}
public E first()
{
return data[front];
}
public E dequeue()
{
if(isEmpty())
{
System.out.println("queue is empty");
return null;
}
E ans=data[front];
data[front]=null;
front=(front+1)%data.length;
size--;
return ans;
}
public void enqueue(E e)
{
if(size==data.length)
{
System.out.println("size is full");
return;
}
data[(front+size)%data.length]=e;
size++;
}
}
public class ArrayQueueTest
{
public static void main(String[] args)
{
System.out.println("welcome");
ArrayQueue <Integer>aq=new ArrayQueue<Integer>(5);
aq.enqueue(new Integer(5));
aq.enqueue(new Integer(6));
aq.enqueue(new Integer(0));
aq.enqueue(new Integer(8));
System.out.println(aq.size());
for(int i=0;i<aq.size();i++) //loop to print the data of queue
{
// Object ob=aq.data[i]; //why i will get an exception if i did not make a comment to this line
System.out.println(aq.data[i]); /*why i am getting a ClassCastException getting at this line */
}
}
}
推荐答案
您正在忽略编译时警告。
You are ignoring compile time warnings. This is never a good sign.
警告基本上告诉您,您不能使用 E [] $ 进行强制转换。
The warning basically tells you that you cannot make a cast using E[]
. This cast is basically removed during the compile time process with a warning.
data
现在基本上变成了 Object []
数组在运行时使用,并且在这种情况下使用,编译器在需要强制转换的地方添加强制转换,例如(E),例如 Integer i = (整数)aq.dequeue();
。当您访问数组时,例如(((Integer [])aq.data)[i]
),Java也会执行此操作,这实际上是在执行期间删除泛型的结果
data
now basically becomes a Object[]
array at runtime, and is used at such, the compiler adds casts like (E) at places where a cast is needed, such as Integer i = (Integer)aq.dequeue();
. Java also does this when you access the array, such as ((Integer[])aq.data)[i]
, this is really the effect of that generics are removed during compile time.
虽然Java可以正确地帮助您,但它还显示 Object []
不是 Integer []
。如果Java在编译时没有删除泛型,它将在当前警告所在的行出错。
While java helps you correctly, it also shows you that a Object[]
is not a Integer[]
. If java didn't remove the generics at compile time, it would error out at the line where the warning now is.
您应该解决数据
问题,方法是提供2种方法,例如 Object [] Collections.toArray()
和 E [] toArray(E [])
You should solve your data
problem by providing 2 methods like Object[] Collections.toArray()
and E[] toArray(E[])
这篇关于我的Java应用程序中的Caugth ClassCastException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!