Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Caugth ClassCastException in my Java application

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 */
        }
    }
}
like image 388
Siddharth Avatar asked Sep 26 '26 22:09

Siddharth


1 Answers

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[])

like image 82
Ferrybig Avatar answered Sep 28 '26 23:09

Ferrybig



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!