Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java System.out.format with double array

Tags:

java

printf

I am trying to output information with System.out.format using a double[] array as the argument. This does not work:

out.format("New dimensions:\n" +
        "Length: %f\n" +
        "Width: %f\n\n",
        doubleArray);

This, however, does:

out.format("New dimensions:\n" +
        "Length: %f\n" +
        "Width: %f\n\n",
        doubleArray[0], doubleArray[1]);

Why doesn't the first format work? It supposedly works with strings just fine.

like image 575
joelises Avatar asked Dec 02 '25 19:12

joelises


1 Answers

Java will autobox your double to a Double, but it won't autobox your double[] to a Double[], so it doesn't match Object[]. As a result, instead of being unpacked into the Object... varargs, your array is being treated as the array itself -- which, obviously, can't be formatted as a double.

If you declare your array as Double[] instead of double[], the call to format works.

like image 96
Etaoin Avatar answered Dec 05 '25 08:12

Etaoin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!