Question: Hi! I am getting this error in my code: Exception in thread main java.lang.ClassCastException: class [Ljava.lang.Object; cannot be cast to class [Ljava.lang.Integer; ([Ljava.lang.Object; and [Ljava.lang.Integer;
Hi! I am getting this error in my code:
Exception in thread "main" java.lang.ClassCastException: class [Ljava.lang.Object; cannot be cast to class [Ljava.lang.Integer; ([Ljava.lang.Object; and [Ljava.lang.Integer; are in module java.base of loader 'bootstrap') at Homework2.main(Homework2.java:54)
Anyone know how to fix this? I think it has something to do with my toArray method. The lesson is down below as well as my current code and the two different toArray methods. The one I implemented is not correct.

import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.function.Function; public class Homework2 { //This is the correct implementation of map function, corrected the syntax //this will take a Function (with input type - D and output type R), and an array //of type D, returns an array of type R after applying all elements with the //function public static R[] map(Function function, D[] array ){ //creating an Object array of input array's capacity, casting it to R[] List newList = new ArrayList(); List oldList = Arrays.asList(array); //iterating through all elements in input array for(D e : oldList) { newList.add(function.apply(e)); } //returning resultant array R[] result = (R[]) new Object[newList.size()]; return newList.toArray(result); } //method to print contents of an array of generic type R public static void PrintArray(R[] array){ for(int i=0;i function1= new CalculateSuccessor(); Integer[] integerArray = {1, 3, 4, 2, 5}; //mapping and printing the array after applying the function PrintArray(map(function1, integerArray)); //Returns 2 4 5 3 6 //Example 2 Function function2=new CalculateLength(); String[] stringArray={"Java", "C++", "Smalltalk"}; //mapping and printing the array after applying the function PrintArray(map(function2, stringArray)); //Returns 4 3 9 //Example 3 Function function=new CalculateTriple(); Double[] doubleArray={2.0,4.0,5.0,1.0}; //mapping and printing the array after applying the function PrintArray(map(function, doubleArray)); //Returns 6.0 12.0 15.0 3.0 } }
// CalculateLength.java (returns length of the String) import java.util.function.Function; /** * Function class to find the length of an input String value. */ class CalculateLength implements Function { @Override public Integer apply(String t) { return t.length(); } }import java.util.function.Function; /** * Function class to find the length of an input String value. */ class CalculateSuccessor implements Function { @Override public Integer apply(Integer t) { return t+1; } } While Java does not support higher-order functions, we recently had an introduction to emulating them using the edu.psu. cmpsc221.Function interface. Functional programming languages have a concept called map that takes a function and an array as parameters, and it returns the array produced by ap- plying the function to every element of the original array. Given that our recent introduction to the edu.psu.cmpsc221.Function interface used static members, our map function wil also be static and will have the following signature: public static R map(Function function, D] array) This assignment requires you to implement the map function specified; please implement it in a class named edu.psu.cmpsc221.Homework2. The syntax for this generic function is slightly different from the syntax of the other Java generics that we have seen in class (there is an additional pair of type variables that follows the static keyword). This is the syntax Java uses for a static generic method. Please also implement a PrintArray method that takes an array of reference types as a parameter and simply prints the elements of the array to the screen. PrintArray shall be generic. To help clarify what map is doing, a few example uses are provided // Example 1 Function function - new CalculateSuccessorO; Integer integerArray 11, 3, 4, 2, 5J PrintArray (map(function, integerArray)); // map returns 12, 4, 5, 3, 6 // Example 2 Function anotherFunction - new CalculateLengthO; String[] stringArray { "Java", "C++", "Smalltalk" }; PrintArray (map (anotherFunction, stringArray)) // map returns 14, 3, 9 // Example 3 Function tripleFunction-new CalculateTriple(); Double[] doubleArray = { 2.0, 4.0, 5.0, 1.0 }; PrintArray (map (tripleFunction, doubleArray)); // map returns 6.0, 12.0, 15.0, 3.0 Please note that while the solution is actually quite short, this is a challenging problem because Java will likely get in your way and prevent you from implementing "obvious" solutions (recall that Java generics are known to not play nicely with arrays). Lecture has introduced you to everything that you need to solve this problem. While you are always welcome to research any solutions on the Internet (cite your sources!!1!), please note that the submitted solution must respect the signature specified in the assignment My best suggestion to you is to start working on this problem early. Don't wait. No, seriously, get moving! Fire away with any questions that may arise. objectl toArray ) Returns an array containing all of the elements in this list in proper sequence (from furst to last element). toArray TII a) Returns an array contatning all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the spectfled array While Java does not support higher-order functions, we recently had an introduction to emulating them using the edu.psu. cmpsc221.Function interface. Functional programming languages have a concept called map that takes a function and an array as parameters, and it returns the array produced by ap- plying the function to every element of the original array. Given that our recent introduction to the edu.psu.cmpsc221.Function interface used static members, our map function wil also be static and will have the following signature: public static R map(Function function, D] array) This assignment requires you to implement the map function specified; please implement it in a class named edu.psu.cmpsc221.Homework2. The syntax for this generic function is slightly different from the syntax of the other Java generics that we have seen in class (there is an additional pair of type variables that follows the static keyword). This is the syntax Java uses for a static generic method. Please also implement a PrintArray method that takes an array of reference types as a parameter and simply prints the elements of the array to the screen. PrintArray shall be generic. To help clarify what map is doing, a few example uses are provided // Example 1 Function function - new CalculateSuccessorO; Integer integerArray 11, 3, 4, 2, 5J PrintArray (map(function, integerArray)); // map returns 12, 4, 5, 3, 6 // Example 2 Function anotherFunction - new CalculateLengthO; String[] stringArray { "Java", "C++", "Smalltalk" }; PrintArray (map (anotherFunction, stringArray)) // map returns 14, 3, 9 // Example 3 Function tripleFunction-new CalculateTriple(); Double[] doubleArray = { 2.0, 4.0, 5.0, 1.0 }; PrintArray (map (tripleFunction, doubleArray)); // map returns 6.0, 12.0, 15.0, 3.0 Please note that while the solution is actually quite short, this is a challenging problem because Java will likely get in your way and prevent you from implementing "obvious" solutions (recall that Java generics are known to not play nicely with arrays). Lecture has introduced you to everything that you need to solve this problem. While you are always welcome to research any solutions on the Internet (cite your sources!!1!), please note that the submitted solution must respect the signature specified in the assignment My best suggestion to you is to start working on this problem early. Don't wait. No, seriously, get moving! Fire away with any questions that may arise. objectl toArray ) Returns an array containing all of the elements in this list in proper sequence (from furst to last element). toArray TII a) Returns an array contatning all of the elements in this list in proper sequence (from first to last element); the runtime type of the returned array is that of the spectfled array