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
doubleto aDouble, but it won’t autobox yourdouble[]to aDouble[], so it doesn’t matchObject[]. As a result, instead of being unpacked into theObject...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 ofdouble[], the call toformatworks.