Detect if array contains integer or double

I am working on a project that requires me to have a string representation of an array. The problem is that this duplicate code, I am sure, can be reorganized in some way, but I have not found it yet.

private static String printDoubleArray(String title, double[] array){ String result = title; for (double d : array) { result += d + " "; } return result; } private static String printIntArray(String title, int[] array){ String result = title; for (int d : array) { result += d + " "; } return result; } 

Thanks in advance.

+7
source share
6 answers

Why not use one of the Arrays.toString(...) methods from the java.util package?

 int[] intArray = {1, 2, 4}; double[] doubleArray = {1.1, 2.2, 4.4}; System.out.println(Arrays.toString(intArray)); System.out.println(Arrays.toString(doubleArray)); 
+2
source

You can use java.lang.reflect.Array , which provides access to elements of any time in the array. See get(arr, index) , getLength(arr) , etc.

+4
source

The solution to your problem is this:

 public class Test { public static String numberArrayToString(Number[] arr) { if ( arr == null ) return ""; StringBuilder sb = new StringBuilder(); for ( Number n : arr) { sb.append(n.toString()).append(" "); } return sb.toString(); } public static void main(String[] args) { Integer[] intArr = { 1, 4, 6, 7 }; Double[] dblArr = { 33.44, 55.66, 11.22 }; System.out.println(numberArrayToString(intArr)); System.out.println(numberArrayToString(dblArr)); } } 

He produces this:

 1 4 6 7 33.44 55.66 11.22 

However, it only works if you define your arrays using boxed primitives (i.e. subclasses of Number ), but not with primitives (i.e. int[] and double[] ).

The problem is that using boxed primitives is really inefficient.

+2
source

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)); 
+2
source

Using guava, you can convert a primitive array (int []) to a list of shell type (List) using Ints . Alternatively, you can use Joiner .

 Joiner.on(' ').join(Ints.asList(new int[]{1,2,3})); 

Then the method is used:

 void printIntArray(String title, int[] array) { printArray(title, Ints.asList(array)); } String printArray(String title, Iterable<? extends Number> asList){ return title + " " + Joiner.on(' ').join(asList); } 
+1
source

You can use the Integer and Double wrapper classes instead of int and double. Write one method using your base class Number

0
source

All Articles