Explanation
You should replace it with:
d[i] = Double.valueOf(d.length - i);
From its Javadoc:
Deprecated.
It is rarely appropriate to use this constructor. The static factory valueOf(double)
is generally a better choice, as it is likely to yield significantly better space and time performance.
In general, valueOf
is not forced to always return a new instance. It can utilize an internal cache and re-use values created before already, which makes it faster. For example if you create hundreds of 1.0
.
Note
Is there a specific reason you are using a Double[]
in the first place? If not, go for double[]
instead. The primitives are much faster and have less memory overhead, compared to their object wrapper.
Then your code is just:
double[] d = new double[10];
for (int i = 0; i < d.length; i++)
d[i] = d.length - i;
By the way, you should prefer to never omitt the curly braces. Even if your loop is just one line. This is a very common source for bugs that are hard to find.
Also, your variable naming is not very good. What is d
? Try to give it a name that reflects what it actually means. Like ages
if it stores person ages, for example. If you do not have something specific, maybe use values
. That is already better than just d
. Especially since it is plural, so it is clear that it is an array of multiple values.
double[] values = new double[10];
for (int i = 0; i < values.length; i++) {
values[i] = values.length - i;
}