如何将 Generic ArrayList 转换为 Generic Array

当我调用printArraypvsm 时,我不断收到的错误是:


Exception in thread "main" java.lang.ClassCastException: 

    java.base/[Ljava.lang.Object; cannot be cast to java.base/[Ljava.lang.Integer

我知道问题出在R[] result = (R[]) list.toArray(). 我不知道如何将 ArrayList 转换为数组并同时将其转换为泛型。注意我不能更改函数的参数map或添加任何新函数。


public class Homework2 {


    public static void main(String[] args){

        Function<Integer,Integer> function = new CalculateSuccessor();

        Double[] d= {2.0,4.0,8.0};

        Integer[] i= {2,4,8};

        printArray(map(function,i));

    }


    @SuppressWarnings("unchecked")

    public static <R,D> R[] map(Function<R,D> function, D[] array){

        ArrayList<R> list = new ArrayList<>();

        for (D element: array){

           list.add(function.apply(element));

        }



        // THIS LINE OF DAMN CODE

        R[] result = (R[]) list.toArray();


        return result;

    }


    public static <R> void printArray(R[] array){

        System.out.print("{ ");

        for (R element: array){

            System.out.print(element + ", ");

        }

        System.out.print("}");

    }


    public static class CalculateSuccessor implements Function<Integer,Integer> {

        @Override

        public Integer apply(Integer parameter) {

            return parameter * 2;

        }

    } //End CalcSuc


} //End Homework2

在另一堂课我有


public interface Function<R,D> {

     public R apply(D parameter);

}

你需要的function.apply。我的教授坚持我们使用它而不是导入函数。


MM们
浏览 137回答 2
2回答

缥缈止盈

第一部分,您需要Class<R>为了动态创建数组R[]。我更愿意Arrays.toString实现我自己的版本。我还需要一个Function<D, R>(不是一个Function<R, D>)。但是做出这些改变就像public static void main(String[] args) {&nbsp; &nbsp; Function<Integer, Integer> function = new CalculateSuccessor();&nbsp; &nbsp; Double[] d = { 2.0, 4.0, 8.0 };&nbsp; &nbsp; Integer[] i = { 2, 4, 8 };&nbsp; &nbsp; System.out.println(Arrays.toString(map(Integer.class, function, i)));}public static <R, D> R[] map(Class<R> cls, Function<D, R> function, D[] array) {&nbsp; &nbsp; ArrayList<R> list = new ArrayList<>();&nbsp; &nbsp; for (D element : array) {&nbsp; &nbsp; &nbsp; &nbsp; list.add(function.apply(element));&nbsp; &nbsp; }&nbsp; &nbsp; return list.toArray((R[]) Array.newInstance(cls, list.size()));}我得到[4, 8, 16]

富国沪深

您可以从 中提取类型信息,Function<D,R>因为您使用实际类实现了它。所以与@Elliott Frisch 一起回答。public static <R, D> R[] map(Function<D, R> function, D[] array) {&nbsp; &nbsp; ArrayList<R> list = new ArrayList<>();&nbsp; &nbsp; for (D element : array) {&nbsp; &nbsp; &nbsp; &nbsp; list.add(function.apply(element));&nbsp; &nbsp; }&nbsp; &nbsp; Class<?> componentClass = extractReturnType(function)&nbsp; &nbsp; return list.toArray((R[]) Array.newInstance(componentClass, list.size()));}private static Class<?> extractReturnType(Function<?, ?> function) {&nbsp; &nbsp; Type[] interfaces = function.getClass().getGenericInterfaces();&nbsp; &nbsp; for(Type iface:interfaces) {&nbsp; &nbsp; &nbsp; &nbsp; if (iface instanceof ParameterizedType && Function.class.equals(((ParameterizedType) iface).getRawType())) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return (Class<?>) ((ParameterizedType) iface).getActualTypeArguments()[1];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; throw new IllegalArgumentException("Unable to extract type information");}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go