从UnorderedListADT创建UnorderedVector,但获得未经检查的强制转换错误

时间:2017-07-17 16:10:49

标签: java list data-structures unordered unchecked-cast

在处理这种铸造物体的概念时,我感到非常困惑,但我相信我非常接近完成。如果你可以请看看我的代码,并告诉我如何停止接收这个错误,它会很棒。

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

这是我在第一个回复建议后的第二次尝试(但仍然收到未经检查的强制转换错误):

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

1 个答案:

答案 0 :(得分:0)

我没有看到"存储的定义,"但我认为它是一个数组。您不能将一种类型的数组转换为另一种类型的数组。您只能将Array转换为超类,例如Object。