You need to create an empty array of 10 integers and fill it with values ​​using a loop, each of which will be 3 times more than the previous one (starting with 1), that is, get [1, 4, 7, 10, 13, 16, 19, 22, 25, 28] .

I wrote this code:

 public class massiv { public static void main(String[] args) { int[] array = new int[10]; array[0] = 1; for (int i = 0; i < array.length; i++) { array[i] = array[i] + 3; //вот тут туплю - не пойму как решить System.out.println(array[i]); } } } 

How to fill the array?

  • array[i] = i*3 + 1; - Yaant

5 answers 5

Loop from the second element of the array.

 public static void main(String[] args) { int[] array = new int[10]; array[0] = 1; for (int i = 1; i < array.length; i++) { array[i] = array[i - 1] + 3;//вот тут туплю - не пойму как решить System.out.println(array[i]); } } 

Before the loop begins, we assign a value to the element [0]. Then we start the cycle with the element [1]. At each iteration step, we use the previous element to add 3 to it and assign it to the next element.

So the cycle works:

array [1] = array [0] + 3. Get array [1] = 4.

Then array [2] = array [1] +3. We get array [2] = 4 + 3 = 7, well, etc.

  • explain to the author why array [i-1] - Senior Pomidor
  • so kind of explained below in kamentah. OK, try # 2)) Before the cycle begins, we assign a value to the element [0]. Then we start the cycle with the element [1]. at each iteration step, we use the previous element to add 3 to it and assign it to the next element. So it works: array [1] = array [0] + 3. Get array [1] = 4. Then array [2] = array [1] +3. We get array [2] = 4 + 3 = 7, well, etc. - KAGG Design
  • one
    That’s why we’ve got it here to объясни how to дополни ответ . Useful info should not be collected in the comments. - vp_arth
  • Lada, now I will throw there - KAGG Design

Since the given recursive function

 F(0) = 1; F(n) = F(n - 1) + 3; 

You can replace with non-recursive:

 F(n) = n * 3 + 1; 

This can be done a little shorter with a binding of the value to the iteration number ( i ):

 int[] array = new int[10]; for (int i = 0; i < array.length; i++) { array[i] = i * 3 + 1; } 

And in the case of using Java 8:

 int[] array = IntStream.range(0, 10).map(e -> e * 3 + 1).toArray(); 

    array[i] = array[i]+3; // I'm stupid here - I don't understand how to solve

    yes, this is where the snag is, you probably want to increase the value of the previous element by 3. To do this, you can do this:

      array[i] = array[i-1]+3; 

    However, in this case, you need to start the loop not from the zero element of array [0], but from the first array [1], because the element of array[i-1] (ie, array[0-1] ) does not exist

       public static void main(String[] args) { int[] array = new int[10]; int value = 1; for (int i = 0; i < array.length; i++) { array[i] = value; value += 3; System.out.println(array[i]); } } 
         for (int i = 0; i < array.length; i++) { 
         for (int i = 1; i < array.length; i++) { 
         array[i] = array[i] + 3; 
         array[i] = array[i-1] + 3; 
        • What angle should you look at? - Regent
        • @Regent, read the line in the quote, read the line outside the quote, find the changes. Repeat for the second quote. You understand and go to edit the code :) - Qwertiy
        • It is not the answer to the question. To leave your comments or ask the author to clarify, leave a comment to the appropriate post. - From the queue of checks - vp_arth
        • @vp_arth, why not? - Qwertiy
        • Although this code may be a solution to the problem, the answer should contain information about why it solves the problem. In addition, at that time when this answer was written, more detailed similar answers were already present. - vp_arth