Caugth ClassCastException in my Java application

淺唱寂寞╮ 提交于 2019-12-23 19:40:35

问题


A queue implementaion using an array but i m getting an exception.

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 */
        }
    }
}

回答1:


You are ignoring compile time warnings. This is never a good sign.

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 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.

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.

You should solve your data problem by providing 2 methods like Object[] Collections.toArray() and E[] toArray(E[])



来源:https://stackoverflow.com/questions/35195986/caugth-classcastexception-in-my-java-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!