I would use commons-lang to create a string by creating a print method using the Object[] array as such
int[] intArray = ...; String str = printArray(title, ArrayUtils.toObject(intArray)); double[] doubleArray = ...; String str = printArray(title, ArrayUtils.toObject(doubleArray)); public static void printArray(String title, Object[] array) { return title + " " + StringUtils.join(array, " "); }
Note that this will internally copy the array that will insert the int into the Integer objects, so if the array performance / size is a problem, I would bite the bullet and create methods for primitive types, although I would call all the printArray methods and overload it with different types .
EDIT:
Instead of commons-lang, you can use Guava primitives that will not copy the array (but will simply autoblock the floats to the list) so you can:
int[] intArray = ...; String str = printArray(title, Ints.asList(intArray)); double[] doubleArray = ...; String str = printArray(title, Floats.asList(doubleArray));
beny23
source share