![](/img/trans.png)
[英]Ljava.lang.Object; cannot be cast to [Ljava.lang.Integer
[英]Ljava.lang.Object; cannot be cast to [Ljava.lang.Integer;
map函數只是簡單地遍歷整數數組並將函數應用於它,然后將其添加到輸出數組。 我收到此錯誤,但似乎找不到將對象投射到Integer的位置。 map函數返回一個Integer數組,並發送到采用一個Integer數組的printArray。 有任何想法嗎?
public static void main(String[] args)
{
Function<Integer,Integer> function = new CalculateSuccessor<Integer,Integer>();
Integer[] integerArray={1,3,4,2,5};
printArray(map(function, integerArray));
}
我刪除了其余代碼,因為發現解決方案是Function之后的<Integer, Integer>
。
我假設您的printArray方法需要一個Object []
即使Object是Integer的超類,Object []也不是Integer []的“超級”實例的定義。
假設此代碼有效:
Object[] array = new Integer[10];
那么這也將是有效的
array[0] = new Car("Mercedes");
但是后者應該是不可能的。 因此,對數組的“繼承”限制。
清單也一樣
List<Object> myList = new ArrayList<Integer>();
它會給您一個編譯器錯誤。
這是因為您使用泛型Function<Integer,Integer>
番石榴試圖轉換您傳遞給Integer的值,但實際上是傳遞Object的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.