如何将特定数字添加到数组中的每个元素? Java

How to add specific numbers to each element in an array? Java

我正在尝试将 10 添加到数组的每个元素,但我得到:

[-10.0, -9.0, -8.0, -7.0, -6.0, -5.0, -4.0, -3.0, -2.0, -1.0, 0.0, 1.0, 2.0]

我要的是

[-10.0, 0.0, 10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, 80.0, 90.0, 100.0, 110.0]

如有任何帮助,我们将不胜感激。 这是我的代码:

import java.util.Arrays;

public class ReportCardClient {

    public static void main(String[] args) {

        double[] a1 = new double[13];

        for(int i=-0; i < a1.length; i++) {
             a1[i]=i-10; 

        }


        System.out.println(Arrays.toString(a1));

        /*
         * create an array marks for 13 double values
         * assign values, using a loop, such that,
         * first value is -10
         * second value is 0
         * third value is 10
         * fourth value is 20
         * ...
         */

试试这个:

for(int i=0; i < a1.length; i++) {
     a1[i]=(i-1)*10; 

}

Gendarme 的另一种解决方案是将 a1[i]=i-10; 替换为 a1[i]=(10*i)-10;