This question already has an answer here:
If I have a reference of type Object and I know that this is an array, but I don't know what kind of array, what is the best way to generate a nice String?
Or in other words, what is the best / nicest implementation for the following method:
private static String anyArrayToString(Object someArray) {
if (!someArray.getClass().isArray()) {
throw new IllegalArgumentException("someArray is not an array!");
}
return ???;
}
I am aware of the methods from Arrays, namely Arrays.toString(int[]), Arrays.toString(double[]), etc. These do not work here because they are strongly typed and I don't feel like having a big if-else-if cascade. (although this is my plan B if no better solution can be found)
Ideally I would want a method that works on Object references such as System.arraycopy which checks dynamically if the passed reference is an array or not.
why do I not know what kind of array it is?
Because I get the object reference via reflection after iterating over all get-methods of a different object. It is part of a utility method that finds arbitrary differences between two complex objects even if these differences are nested deep.
Thank you very much.
Aucun commentaire:
Enregistrer un commentaire