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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With