I have a method which converts the Object ( which is an array of primitive or strings) which i have got after invocation of a method (so its in instance of java.lang.Object). I want to build the ArrayList from the input array.
I tried using new ArrayList
I have coded something like this
private List<String> getListOfStringForPrimitives( Object inputObject ) {
Class<?> inputClass = null;
if (inputObject != null) {
inputClass = inputObject.getClass();
// Returns true if the inputObject is an Array
if (isTypeAnArray(inputClass.getName())) {
Class<?> componentType = inputClass.getComponentType();
// If the inputObject is array of primitives build the list of Strings from the inputObject
if (isTypePrimitive(componentType.getName())) {
//ArrayList <String> arryList;
// build an array list of Strings. from the inputObject and return.
}
}
}
return arryList;
}
I am stuck how to build the ArrayList from input array!
Thanks in advance.
Use
java.lang.reflect.Arrayto get the primitive values from the primitive array, and then add the boxed equivalents (e.g.Integer) to the ArrayList.Write a set of if-then-else clauses that compare componentType to
Integer.TYPE,Character.TYPE, etc. In each clause, write the appropriate call to the Array class to get the primitives, and then pass them to Whatever.valueOf, and then stick the results into your Array List.