当我去运行它时,我得到了错误:
线程“main”中的异常 java.lang.ClassCastException: [Ljava.lang.Object; 不能转换为 [Ljava.lang.Integer; 在 Main.main(Main.java:30)
public class Main
{
private interface Function<R, D>
{
public R apply(D parameter);
}
public static void main(String[] args)
{
// Example 1
Function<Integer, Integer> function = new CalculateSuccessor();
Integer[] integerArray = {1, 3, 4, 2, 5};
PrintArray(map(function, integerArray)); // map returns {2, 4, 5, 3, 6} <--- line 30
}
@SuppressWarnings({"unchecked"})
public static <R, D> R[] map(Function<R, D> function, D[] array)
{
R[] results = (R[]) new Object[array.length];
// Iterate through the source array, apply the given function, and record results
for (int i = 0; i < array.length; i++)
{
results[i] = (R)function.apply(array[i]);
}
return results;
}
public static <T> void PrintArray(T[] array)
{
for (T element : array)
{
System.out.println(element.toString());
}
}
}