开发者

Detect if an array contains integer or double

I'm working on a project that requires me to have a string representation of an array. The problem is having this duplicated code, that I'm sure can be refactored in some way, but I haven't found one 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.


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


Why not use one of the methods Arrays.toString(...) from 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));


The solution to your issue 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));

    }

}

It produces this:

1 4 6 7 
33.44 55.66 11.22 

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

The problem is that using boxed primitives is really inefficient.


I would use commons-lang to create the string create the print method using an 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, this internally will copy an array which will box the ints into Integer objects, so if performance/size of array is an issue I would bite the bullet and create methods for the primitive types, though I'd call all the methods printArray and overload it with different types.

EDIT:

Instead of commons-lang, you could use Guava primitives, which will not copy the array (but will just autobox the floats in the list), so you could do:

int[] intArray = ...;
String str = printArray(title, Ints.asList(intArray));

double[] doubleArray = ...;
String str = printArray(title, Floats.asList(doubleArray));


Using guava you can convert the primitive array (int[]) to a List of the wrapper type (List) using Ints. Additionally you can use the Joiner.

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

The method is then:

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);
}


You can use the wrapper classes Integer and Double instead int and double. Write one Method using their base class Number

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜