Home>

I'm afraid I have questions.

Substitute the array of 1 to 10 into the function times.
Create function itmes that returns times when arrayed times
It is a problem,

class Chap1_4_1 {
public static void main(String[] args) {
int[] times = {1,2,3,4,5,6,7,8,9,10};
Substitute the array of 1 to 10 into the function times.
System.out.println("times is "+ times);
}

public static int items(int times) {
int times = times * 2;
return times;
Create function itmes that returns times when arrayed times
}
}

I was interpreting that, but I get an error no matter how many times I try.
I know that I am stumbling in the method part, but I can not understand it no matter how many times I do it.

What kind of procedure should I use to write a program for this kind of problem?
I am a beginner, so I would appreciate if you could kindly teach me.

Substitute the array of 1 to 10 into the function times.

  • Answer # 1

    I intentionally use the for statement,

      public int[] items(int[] times) {
            // prepare an array with the same length as the argument
            int[] values ​​= new int[times.length];
            // repeat for the length of the array
            for(int i=0;i<times.length;i++) {
                values[i] = times[i] *2;
            }
            return values;
        }

    Is this all right if the contents of the array are simply doubled and returned?