1

When I go to run this I get the error:

Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Integer; at 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());
        }   
    } 
}
7
  • Presumably line 30 is the one that says R[] results = (R[]) new Object[array.length]; ? Commented Oct 3, 2013 at 3:23
  • Line 30 is when I call the PrintArray method. My apologies for not stating that. Commented Oct 3, 2013 at 3:29
  • 1
    My guess is that R is being resolved to Object, not to Integer as you expect. Honestly, arrays and generics don't usually play nicely together. If I had to write this, I would have made map return a List<R> instead of an R[] - it just seems cleaner somehow. Commented Oct 3, 2013 at 3:44
  • 1
    I ran the code with my own Function(which just returns the parameter) it ran fine. intellij 12 - jdk1.7.0_21 Commented Oct 3, 2013 at 3:54
  • It may be that the issue is in the definition of CalculateSuccessor - maybe this constrains R somehow. Commented Oct 3, 2013 at 4:01

1 Answer 1

0

Problem is the result array is created as new Object[array.length]. The cast (R[]) doesn't convert this array to a different type, it just tells the compiler to treat it like R[]. See Quick Java question: Casting an array of Objects into an array of my intended class. Creating an array of generic type is a bit nasty - you can google for various solutions, e.g. How to create a generic array in Java? - but generally the simplest approach is to use a list instead of an array: you can create it with new ArrayList<R>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.