This week's book giveaway is in the OCMJEA forum. We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line! See this thread for details.
I have an array whose element type I do not know until runtime. That is, it could be an array to object, array to array, array to primitive. I need a simple method to loop thru the array and test if it contains an element convertible to a certain numeric value. The element include numeric primitives, their wrappers, BigDecimal/BigInteger, any instance of java.lang.Number.
I would use recursion to loop thru the whole array since it may be an array of array. But the problem is how can i test if the array elements are primitive type. Here is my code that failed at line 6 because of casting primitive to Object
The java.lang.Class class has a number of useful methods you're not using. There's isArray() (a vastly better substitute for checking if there's a bracket in the class name!), there's getComponentType() (which tells you the data type of the elements of an array) and there's isPrimitive() which tells you if a class represents a primitive type. So to check if something is a primitive array, you'd do