javalistdata-structuresunorderedunchecked-cast

Making an UnorderedVector from an UnorderedListADT, but getting an unchecked cast error


I'm very confused at the moment when dealing with this concept of casting objects and whatnot, but I believe I am very close to being finished. If you could please take a look at my code and clue me in on what I could do to stop receiving this error, it'd be great.

public E remove(int position){
    position -= 1;
    if(outOfBounds(position))
        throw new RuntimeException("Invalid position.");
    E[] temp;
    temp = (E[])storage[position];// around here is where I receive the error
    currentSize--;
    shiftLeft(position);
    return temp[position];
}// DONE

Here is my second attempt after the first reply's suggestion(however, still receiving an unchecked cast error):

public E remove(int position){
    position -= 1;
    if(outOfBounds(position))
        throw new RuntimeException("Invalid position.");
    E[]temp = (E[])new Object[maxSize];
    temp = (E[])storage[position];
    currentSize--;
    shiftLeft(position);
    return temp[position];}// DONE

Solution

  • I don't see the definition of "storage," but I assume that it is an array. You can't cast an array of one type to an array of another type. You can only cast Array to a superclass, such as Object.