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());
        }   
    } 
}
Java Devil
  • 9,835
  • 7
  • 30
  • 44
  • Presumably line 30 is the one that says `R[] results = (R[]) new Object[array.length];` ? – Dawood ibn Kareem Oct 03 '13 at 03:23
  • Line 30 is when I call the PrintArray method. My apologies for not stating that. – Jake Klapper Oct 03 '13 at 03: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` instead of an `R[]` - it just seems cleaner somehow. – Dawood ibn Kareem Oct 03 '13 at 03:44
  • 1
    I ran the code with my own Function(which just returns the parameter) it ran fine. intellij 12 - jdk1.7.0_21 – BevynQ Oct 03 '13 at 03:54
  • It may be that the issue is in the definition of `CalculateSuccessor` - maybe this constrains `R` somehow. – Dawood ibn Kareem Oct 03 '13 at 04:01
  • If I pass an array of integers to map and then cast that to be an array of objects, is there a way I can get it back to an array of intergers? Because if my function that I pass to map calculates the length of strings in an array, I would need for map to be able to handle strings as well. That is why I thought to cast all arrays in map to be objects. – Jake Klapper Oct 03 '13 at 04:08
  • Seriously, as I suggested earlier (and as Mike repeated in his answer), use `List` instead of `R[]`. Instantiate it as `ArrayList`. It will make everything work much more easily. – Dawood ibn Kareem Oct 03 '13 at 04:15

1 Answers1

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>

Community
  • 1
  • 1
Mike Stockdale
  • 5,201
  • 3
  • 27
  • 33